How to know if a key is pressed during MouseDown

  • Thread starter Thread starter active
  • Start date Start date
A

active

I can use Control.ModifierKeys to determine if a modifying key is pressed
when executing MouseDown event, but how can I determine if an "M" was
pressed??



Possible? Can't find a clue in Control doc.



Cal
 
Cal,
Possible? Can't find a clue in Control doc.
I wonder if your users would have a clue to press "M" while clicking with a
mouse! As it sounds like a very non-standard user interface.

If you have a real need for this requirement, I would consider handling the
KeyDown & KeyUp events setting a flag when the "M" key is down. Then in the
MouseDown event I would check the flag.

Of course Win32 already does this for you. You should be able to use either
the GetKeyState or GetAsyncKeyState Win32 API to check the state of the "M"
key.

Declare Auto Function GetKeyState Lib "user32.dll" (ByVal vKey As Keys)
As Short
Declare Auto Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As
Keys) As Short

Debug.WriteLine(GetKeyState(Keys.M))
Debug.WriteLine(GetAsyncKeyState(Keys.M))

Note look up GetKeyState & GetAsyncKeyState for specifics on the value
returned.

Note: The above functions may already be exposed in the Framework, however I
have not come across them yet.

Hope this helps
Jay
 
GetAsyncKeyState seems to fit perfectly. Just check for <0 to see if it's
down at the time of the check. Is the value of "Keys.something" exactly the
same a the virtual key code o "something"?

Thanks a lot
Cal
 
Cal,
Is the value of "Keys.something" exactly the
same a the virtual key code o "something"?
According to Charles Petzold's book "Programming Microsoft Windows With
Microsoft Visual Basic .NET" from MS Press it is.

The Keys Enum is all the valid virtual key that you can pass to
GetAsyncKeyState, which means you can use it to check for Function Keys,
Caps Lock, Scroll Lock and other and any other non-char key you want.

I was originally going to give you:

Declare Auto Function GetKeyState Lib "user32.dll" (ByVal vKey As Char)
As Short
Declare Auto Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As
Char) As Short

However you would have problems trying to check for CapsLock and you would
have to be certain to use only upper case letters. You could also make vKey
as Short, however then you would need to know all the "magic" numbers, Keys
Enum just happens to define all the magic numbers for you. I find using an
existing Enum or defining my own Enum very beneficial when defining API
calls...

Hope this helps
Jay
 
Either it was not out at that time or I it was and I didn't know it so I
bought his C# book some time ago.

Thanks for the info
Cal
 
Back
Top