Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically toggle CAPSLOCK or prevent it from being toggled by the keyboard?

Tags:

c++

windows

I've got a program written in C++ and running on windows. I allow the user to keybind the CAPSLOCK key so it would be nice if, every time they pressed it, they weren't also toggling their CAPSLOCK state.

I've found no way to "capture" the CAPSLOCK message in order to prevent it from being registered by windows. It seems like by the time I get a WM_KEYDOWN message, the indicator light on my keyboard has already switched.

I did find this suggested code:

keybd_event( VK_CAPITAL, 0x3a, KEYEVENTF_EXTENDEDKEY, 0 );
keybd_event( VK_CAPITAL, 0x3a, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0 );

But, it doesn't work. It kinda caused the indicator light to flicker, and doesn't seem to affect the CAPSLOCK state at all.

like image 383
Raptormeat Avatar asked Oct 17 '25 19:10

Raptormeat


1 Answers

Try it in reverse order:

keybd_event( VK_CAPITAL, 0x3a, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0 );
keybd_event( VK_CAPITAL, 0x3a, KEYEVENTF_EXTENDEDKEY, 0 );
like image 83
johndoe Avatar answered Oct 20 '25 10:10

johndoe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!