Brian Rathbone wrote:
I have a canvas subclass, and in the keydown event I need to determine
if the key is an extended key. The purpose is to determine if I need to
use the KEYEVENTF_EXTENDEDKEY flag to simulate the same keystroke.
I needed to be able to distinguish the extended keys, and I also needed
to get the Windows virtual keycode. I didn't want to use ascB(key) or
keyboard.asynchkeydown, mostly since they don't correspond to the
windows virtial keycodes.
In the end, I decided to use the keydown event only as a trigger and
declare GetKeyboardState, which returns the states of all keys on the
keyboard in one shot. Then I can loop through and identify the virtual
keycode of the key that is depressed, and check on the states of
modifier keys. It also allows you to distinguish between the right and
left modifier keys, not that I needed this capability, but it is neat.
Below is a sloppy example I whipped up.
Const VK_PRIOR = &H21
Const VK_NEXT = &H22
Const VK_END = &H23
Const VK_HOME = &H24
Const VK_LEFT = &H25
Const VK_UP = &H26
Const VK_RIGHT = &H27
Const VK_DOWN = &H28
Const VK_INSERT = &H2D
Const VK_DELETE = &H2E
Const VK_SHIFT = &H10
Const VK_CONTROL = &H11
Const VK_MENU = &H12 'ALT key
Const VK_LSHIFT = &HA0
Const VK_RSHIFT = &HA1
Const VK_LCONTROL = &HA2
Const VK_RCONTROL = &HA3
Const VK_LMENU = &HA4
Const VK_RMENU = &HA5
Declare Function GetKeyboardState Lib "user32" Alias
"GetKeyboardState" (pbKeyState As ptr) As integer
dim mbkeys as memoryBlock
dim s as string
dim i as integer
dim extended as boolean
mbkeys = newmemoryBlock(256)
if GetKeyboardState(mbkeys) = 0 then
msgbox "error getting keyboard state"
else
if mbkeys.byte(VK_CONTROL) > 127 then
s = "C"
end
if mbkeys.byte(VK_SHIFT) > 127 then
s = s + "S"
end
if mbkeys.byte(VK_MENU) > 127 then
s = s + "A"
end
for i = 8 to 255 'skip first 7 they are for the mouse
if mbkeys.byte(i) > 127 then 'high order word = 1
//is it extended?
extended = true
select case i
case VK_PRIOR
case VK_NEXT
case VK_END
case VK_HOME
case VK_LEFT
case VK_UP
case VK_RIGHT
case VK_DOWN
case VK_INSERT
case VK_DELETE
else
extended = false
end select
//ignore modifiers
select case i
case VK_SHIFT
case VK_CONTROL
case VK_MENU
case VK_LSHIFT
case VK_RSHIFT
case VK_LCONTROL
case VK_RCONTROL
case VK_LMENU
case VK_RMENU
else
if extended then
msgbox hex(i) + "\\E\\" + s
else
msgbox hex(i)+ "\\" + s
end
exit
end
end
next
end
Thanks to all who responded!
Brian
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>