Difference between KeyCode and KeyData

G

Guest

Hi,

In the event handlers of KeyUp and KeyDown events, the passed-in event
argument has two properties named KeyCode and KeyData. Could anybody tell me
the difference between the two ? The documentation is very vague on this.

And one more question. I'm creating a textbox to let users enter a shortcut
for an action in my application (e.g. it will display the text 'Ctrl - Shift
- A' when users press the corresponding key combination). So, which event(s)
should I catch and how to retrieve all keys the users press, which are not
necessarily simultaneous ?

Thanks in advance

Dotnetjunky
 
C

Claes Bergefall

KeyData contains the modifier codes (i.e. Control, Shift, Alt) or'ed into
the value aswell

Pressing Ctrl+A for instance will give you the following:
KeyCode = Keys.A
KeyData = Keys.A Or Keys.Control
Modifiers = Keys.Control
Control = True
Alt = False
Shift = False

Pressing Ctrl for instance will give you the following:
KeyCode = Keys.ControlKey
KeyData = Keys.ControlKey Or Keys.Control
Modifiers = Keys.Control
Control = True
Alt = False
Shift = False


In your case (Ctrl+Shift+A) handle the KeyDown event and use
any of the following three if-statements:

If e.KeyData = (Keys.A Or Keys.Control Or Keys.Shift) Then
End If

If e.KeyCode = Keys.A AndAlso e.Modifiers = (Keys.Control Or Keys.Shift)
Then
End If

If e.KeyCode = Keys.A AndAlso e.Control AndAlso e.Shift Then
End If


/claes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top