Jump to content

Looking for command-line utility to read keyboard (shift) key state


Recommended Posts

Posted

Hi!

I'm looking for a (free and small) command-line utility to detect shift key status and returning it in exit code.

Has anyone seen something like that on the net?

When executed from a batch program, it should be possible to check

errorlevel to see if the user was pressing SHIFT for instance.

Regards!


Posted (edited)

Why don't I just write one for you right now? :lol:

mov es ax
es
mov al [h417]
and al 3
mov ah h4c
int h21

Return is nonzero if the shift key was held down, otherwise zero.

(free and small)
12 little bytes. I don't think it can get any smaller than that :)

(Remove .txt extension from attachment, it doesn't like the .com extension and ZIP'ing a 12-byte file is pointless.)

SHIFTKEY.COM.txt

Edited by LLXX
Posted

Very good, and really small! :D

This is a good startingpoint!

(Now this topic should be moved to programming I guess?)

I have a problem with detecting the state, though.

It seems to behave like a repeating key (like if you press and hold a normal letter) instead of the keypressed state of a shift key.

Tested like this:

@echo off
:loop
shiftkey& echo %errorlevel%& goto loop

It prints the errorlevel (exitcode from your program) 0's when no shift keys is pressed.

When I push and hold for example the left shift key, it displays '2' once, then 0's for about a second, then more 2's mixed with 0's.

A "real" batch program will do the test at the start, and might likely miss the exact times when the key is detected.

Nevertheless I think what you suggest is the best way to do it (a small asm code)

I guess it should test maybe other bits in the AND statement or another address, but knowing it will take me some time to test and read some, I wanted to thank you at once for this way of doing it! :thumbup

Cheers LLXX!

Posted

The shift key does not repeat. And, according to the PC Technical Reference, 0417 is the only address with the state of the shift keys (lower 2 bits). The other bits there are for Ctrl, Alt, ScrollLock, NumLock, CapsLock, etc.

It only looks like it's repeating because XP's DOS emulation is awful - the state of the shift key is being reported erratically.

I tried with a similar batch file under Win98 and it works perfectly fine.

@echo off
:loop
shiftkey
if errorlevel 0 set errorlev=0
if errorlevel 1 set errorlev=1
if errorlevel 2 set errorlev=2
echo %errorlev%
goto loop

Maybe an equivalent Win32 program might work better, but then again, it is difficult to get realtime key states in multitasking OSs anyway.

If you want, I can write the Win32 program.

Posted

Yes, exactly, the shift keys doesn't repeat, but the test pattern on both Windows 2000 SP4 and XP SP2 matched the typical "rythm" of repeating keys :blink: .

Now TESTED on NT4 server, same result! (So it's not only XP, maybe it's a problem with the 16 bit subsystem or something and I guess Win32 code will work better.)

Haven't touched asm since 386, so I'm in a little over my head here, to say the least! :blushing:

(This problem explains why I had such a hard time finding compiled code, only script examples in VB6 and so on...)

If you're in the mood it would be very interesting to see if the Win32 approach could work!

But if it turns out to be a lot of work, or small hope of success, please don't!.....

Posted (edited)

Seems to fit the bill for me, jaclaz :)

Markus' program checks out OK in XP (and probably others), is 29 Kb, GNU GPL freeware.

Supports a lot of key combinations too, not only shift key, and realtime (fast enough for a batch program anyway).

It detects shift key state even if the user is in another program (expected of course).

I will test the multiuser performance with remote desktop to see if it detects only the console keys when run by a remote user, or if all users input is detected, but I have a feeling the OS handles it.

It's a Win32 program made in delphi. (It might work in Win9x, but I guess it needs the 32bit subsystem installed first.)

@LLXX:

The tiny machine code was very, very neat, but I think this is allso what I was looking for and more!

Thanks for your effort and hope this noob haven't wasted too much of your time... :blushing:

Cheers jaclaz and LLXX! :thumbup

Edited by aquarius
Posted (edited)

A 29Kb "miniapp"?? :blink::o

Well, whatever works for you...

...and by the way, I didn't really spend much more than a few minutes on that last program.

Edited by LLXX
Posted

Hi!

I promised to check out the keyboard state in a multiuser environment, i.e. with several sessions running. Only tested shift key state. Remote PC was Windows 2000 SP4

Tested for

VK_SHIFT (Any shift key, left or right)

VK_LSHIFT (Left shift key)

VK_RSHIFT (Right shift key)

Citrix on NT4 and Remote Desktop on Windows Server 2003:

Remote users VK_SHIFT, VK_LSHIFT and VK_RSHIFT works, and no "leak" from the console or other remote sessions.

Netmeeting Remote Desktop: (W2K/XP)

(Which is not very multiuser, it just takes over console and disables local input)

Remote user shift is detected (VK_SHIFT), but it does not detect the difference between the Right or Left shift keys in this scenario, i.e. a test for VK_LSHIFT or VK_RSHIFT would not work.

TightVNC: (W2K Server)

Same as Netmeeting. You can enable local input in VNC, in which case both remote and local user is detected, as should be expected.

Cheers!

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...