CTRL ALT and SHIFT capture

SonicX_BlueBlur
SonicX_BlueBlur used Ask the Experts™
on
Is there a way to capture the CTRL, ALT, and SHIFT status within a form? Of course, this includes whether you're pressing one or more of these at the same time.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
use GetAsyncKeyState API

Private Declare Function GetAsyncKeyState Lib "user32" Alias "GetAsyncKeyState" (ByVal vKey As Long) As Integer


if (GetAsyncKeyState(vbKeyControl) < 0) then
   msgbox "Control Key is down"
end if

if (GetAsyncKeyState(vbKeyShift) < 0) then
   msgbox "Control Key is down"
end if

if (GetAsyncKeyState(vbKeyAlt) < 0) then
   msgbox "Control Key is down"
end if

Commented:
use GetAsyncKeyState API

Private Declare Function GetAsyncKeyState Lib "user32" Alias "GetAsyncKeyState" (ByVal vKey As Long) As Integer


if (GetAsyncKeyState(vbKeyControl) < 0) then
   msgbox "Control Key is down"
end if

if (GetAsyncKeyState(vbKeyShift) < 0) then
   msgbox "Shift Key is down"
end if

if (GetAsyncKeyState(vbKeyAlt) < 0) then
   msgbox "Alt Key is down"
end if

Commented:
if you want to capture the event immediately when the key is down, easist way is to use a timer control with interval of 100 ms and keep on checking the state of the keys.  Long and complicated way is to use subclassing of the form and detect WM_KEYDOWN message.
Should you be charging more for IT Services?

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
supunr, I'm looking at your answer, but I'm not a whiz at APIs. Where do I put GetAsyncKeyState, in a module, form, what?

Commented:
at the very top of the module in the general section.  if you put that in the module and use code in the form, then declare the function as Public (replace the word private with public), but you can always put these API declarations within the general section of the form itself (but must be private)

Author

Commented:
I may accept your answer, but it doesn't seem Visual Basic recognizes vbKeyAlt. I declared the function in the General Declarations part of the form code (private, as you said) and put the if conditions in the code for a timer that should detect the CTRL, ALT, and SHIFT statuses and act accordingly, but as I said, it tells me that "variable" vbKeyAlt is "not defined".

Commented:
Private Const VK_MENU = &H12 ' alt key
Private Const VK_CONTROL = &H11 ' control key
Private Const VK_SHIFT = &H10 ' shift key

Private Const VK_LCONTROL = &HA2 ' left control key
Private Const VK_RCONTROL = &HA3 ' right control key
Private Const VK_RMENU = &HA5 ' right alt key
Private Const VK_RSHIFT = &HA1 ' right shift key
Private Const VK_LMENU = &HA4 ' left alt key
Private Const VK_LSHIFT = &HA0 ' left shift key

use above constants

Commented:
again, put those constants immediately below the GetAsyncKeyState function in the general section.  As you could see above, you could even differenciate between left and right keys in the keyboard.

Author

Commented:
Good enough for me! ^-^

On a side note, though, if I queried the program for either or both CTRLs and such, could it say that both of each are pressed? Like, if pressing one CTRL increases a variable by one, and the other CTRL would increase that same variable by two, would pressing both increase the variable by three?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial