The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: gavin on September 21, 2005, 02:51:43 PM

Title: Keyboard led's *keybd_event function*
Post by: gavin on September 21, 2005, 02:51:43 PM

My code below lights up the capslock key .
But i have to press it physchically 2 times to turn it off.
Any idea's why this happens?
Because of this i'm not sure how to make it flash on and off using a loop.
Thanks lads.


.data
     
.code

start:
     mov ecx,14h                        ; virtual keycode for CAPSLOCK
     invoke keybd_event,              ;The keybd_event function synthesizes a keystroke.
     ecx,                             ;the key
     NULL,                            ;KEYEVENTF_EXTENDEDKEY not used     
     NULL,                            ;If specified, the key is being released. If not specified,it's being depressed.       
     NULL                             ;additional value associated with the key stroke
     
     invoke ExitProcess,NULL          ;return to windows

end start
Title: Re: Keyboard led's *keybd_event function*
Post by: Tedd on September 21, 2005, 04:43:36 PM
I would guess that it only turns on the led (it doesn't modify the caps state.)
So, the code turns the led on. Then you press caps-lock, which sets caps state and turns the led on (but the led is already on - no visible change.) Then on the second press, caps state is off and the led is turned off.

So I would assume you need to also set caps mode. How you actually do that is another question :bg
The first place I would look is to send a key event (for caps lock being pressed) to the system.


As for making a funky flashy-led thing, you'll need to switch it on, delay for a short time (hint: Sleep) and then turn it off again.
Title: Re: Keyboard led's *keybd_event function*
Post by: MichaelW on September 21, 2005, 06:57:36 PM
The code modifies the caps lock state as well as the LED state, but it's only synthesizing the key-press event. If the function were called a second time with KEYEVENTF_KEYUP in the dwFlags argument then the Caps Lock key would toggle the state on the first press.

Title: Re: Keyboard led's *keybd_event function*
Post by: gavin on September 21, 2005, 07:32:36 PM
Ah right.

Thanks for your help lads it's working like a charm.
I'm going to try get this going to music.


start:
     
     invoke keybd_event,
     14h,
     NULL,
     0,
     NULL


     invoke Sleep,100


     invoke keybd_event,
     14h,
     NULL,
     KEYEVENTF_KEYUP,
     NULL

jmp start


:U