Catch Key Combinations

kadara

New member
Joined
Mar 15, 2013
Messages
3
Programming Experience
Beginner
Hi.

I use the following code to catch some keys>
VB.NET:
[COLOR=#0000ff]If[/COLOR]  e.KeyCode = Keys.F12
      MsgBox("F12 pressed.")
[COLOR=#0000ff]End[/COLOR] [COLOR=#0000ff]If[/COLOR]

I would like to catch the Scroll+F12 combination, but the following code doesn't work:

VB.NET:
[COLOR=#0000ff]If[/COLOR]  e.KeyCode = Keys.F12 AndAlso e.KeyCode = Keys.Scroll
      MsgBox("F12 + Scroll pressed.")
[COLOR=#0000ff]End[/COLOR] [COLOR=#0000ff]If[/COLOR]
 
Keys is a flags enumeration, you can work with it with bitwise operations, for example to combine two values use the Or operator:
Dim combined = Keys.F2 Or Keys.Scroll

That is not typically a combination you would test for through, Keys.Scroll is for Scroll Lock key and is one of the three lock/toogle keys (Num/Caps/Scroll). These are not keys a user press and hold in combination with other keys, only the modifier keys Shift/Ctrl/Alt work like that.
KeyCode event property also does not hold combinations, it only expose the last key pressed, it is the event KeyData property that expose the combination of a Keys value with Shift/Ctrl/Alt.
If you want to check if Scroll Lock is on/off use the My.Computer.Keyboard.ScrollLock property.
 
Back
Top