Catching Keyboard buttons (multiple)

  • Thread starter Thread starter OpticTygre
  • Start date Start date
O

OpticTygre

How would I be able to capture multiple key presses, such as Alt+Shift+D in
a form's KeyPress and KeyDown events? Thanks in advance.

-Jason
 
Hi,

KeyPress event doesn't expose what modifier keys were pressed.

In KeyDown event, use KetEventArgs.Modifiers, KetEventArgs.Control,
KetEventArgs.Shift & KetEventArgs.Alt properties.

HTH

How would I be able to capture multiple key presses, such as Alt+Shift+D in
a form's KeyPress and KeyDown events? Thanks in advance.

-Jason
 
Still not working right.

If I do a:

Debug.WriteLine(e.Modifiers = Keys.Control AndAlso e.Modifiers = Keys.Shift)

Then I get a 'false' for the control key, a 'false' for the shift key, and a
'false' for both keys pressed.

If I do a:

Debug.WriteLine(e.Modifiers = Keys.Control OrElse e.Modifiers = Keys.Shift)

Then I get a 'true' for the control key, a 'true' for the shift key, and a
'false' for both keys pressed.

Any ideas?
 
Debug.WriteLine(e.Modifiers = Keys.Control OrElse e.Modifiers =
Keys.Shift)
'Keys' are bit flags. Use 'Or' to do a bitwise Or. OrElse is a logical
operator not a bitwise operator.

Debug.WriteLine(e.Modifiers = Keys.Control Or e.Modifiers = Keys.Shift)


hope that helps..
Imran.
 
Sortof. The problem is that I'm trying to catch when the ALT and the SHIFT
or CONTROL key is pressed along with the 'D' key. So something like:

If e.Modifiers = Keys.Alt And (e.modifiers = keys.shift or e.modifiers =
keys.control) And e.KeyCode = keys.D Then do something

The problem is, that code doesn't catch the keys properly.

-Jason
 
try something like this:

If (((e.Alt And (e.Shift Or e.Control)) And e.KeyCode = Keys.D) Then
' do something
End If

Or

If ((e.Alt And e.Shift) And e.KeyCode = Keys.D) OrElse _
((e.Alt And e.Control) And e.KeyCode = Keys.D) Then
' do something
End If


hope that helps..
Imran.
 
Ahhhh, yes. Thanks a bunch. It seems that programming it with e.Modifiers
= Keys.control doesn't work properly, but just doing e.control works fine.
Same with the Alt and Shift keys. What a pain. Thanks again.

-Jason
 
It goes like this:

If (e.Control) Then Console.WriteLine ("Control key pressed")
If (e.Alt) Then Console.WriteLine ("Alt key pressed")
If (e.Shift) Then Console.WriteLine ("Shift key pressed")

If (e.Modifiers = (Keys.Control Or Keys.Alt)) Then Console.WriteLine
("Ctrl+Alt pressed")

Modifiers property is of bit-flag type.

Hope this helps.

Still not working right.

If I do a:

Debug.WriteLine(e.Modifiers = Keys.Control AndAlso e.Modifiers = Keys.Shift)

Then I get a 'false' for the control key, a 'false' for the shift key, and a
'false' for both keys pressed.

If I do a:

Debug.WriteLine(e.Modifiers = Keys.Control OrElse e.Modifiers = Keys.Shift)

Then I get a 'true' for the control key, a 'true' for the shift key, and a
'false' for both keys pressed.

Any ideas?
 
Back
Top