KenDickinson
asked on
How do I trap a two key sequence (Ctrl N)
Greetings Experts
I found examples of code to trap a single key, but I haven't been able to make it work when two keys are pressed at the same time, for example the Ctrl key and the N key. Your help is appreciated.
I found examples of code to trap a single key, but I haven't been able to make it work when two keys are pressed at the same time, for example the Ctrl key and the N key. Your help is appreciated.
Place this in your form...
Sample.....
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
Const WM_KEYDOWN As Integer = &H100
Const WM_SYSKEYDOWN As Integer = &H104
If ((msg.Msg = WM_KEYDOWN) Or (msg.Msg = WM_SYSKEYDOWN)) Then
Select Case (keyData)
Case Keys.A To Keys.Z '<--- Catch the Keys you want
MessageBox.Show("You pressed " & ChrW(keyData)) '<--- Then do something
Case Keys.Right
Me.Text = "Right Arrow Captured"
Case Keys.Left
Me.Text = "Left Arrow Captured"
Case Keys.Down
Me.Text = "Down Arrow Captured"
Case Keys.Up
Me.Text = "Up Arrow Captured"
Case Keys.Tab
Me.Text = "Tab Key Captured"
Case (Keys.Control Or Keys.N)
Me.Text = "<CTRL> + N Captured"
Case (Keys.Alt Or Keys.Z)
Me.Text = "<ALT> + Z Captured"
End Select
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
Sample.....
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
Const WM_KEYDOWN As Integer = &H100
Const WM_SYSKEYDOWN As Integer = &H104
If ((msg.Msg = WM_KEYDOWN) Or (msg.Msg = WM_SYSKEYDOWN)) Then
Select Case (keyData)
Case Keys.A To Keys.Z '<--- Catch the Keys you want
MessageBox.Show("You pressed " & ChrW(keyData)) '<--- Then do something
Case Keys.Right
Me.Text = "Right Arrow Captured"
Case Keys.Left
Me.Text = "Left Arrow Captured"
Case Keys.Down
Me.Text = "Down Arrow Captured"
Case Keys.Up
Me.Text = "Up Arrow Captured"
Case Keys.Tab
Me.Text = "Tab Key Captured"
Case (Keys.Control Or Keys.N)
Me.Text = "<CTRL> + N Captured"
Case (Keys.Alt Or Keys.Z)
Me.Text = "<ALT> + Z Captured"
End Select
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
Another hotkey example but it shows how to deal with multiple hotkeys:
https://www.experts-exchange.com/questions/21102359/Message-Loop.html
The sample from planocz will work only when your app has the focus...but that might be what you want.
An alternative to both of these approaches is to use a low level keyboard hook as demonstrated by iboutchkine in this post:
https://www.experts-exchange.com/questions/21258976/KeyPress-Help.html
~IM
https://www.experts-exchange.com/questions/21102359/Message-Loop.html
The sample from planocz will work only when your app has the focus...but that might be what you want.
An alternative to both of these approaches is to use a low level keyboard hook as demonstrated by iboutchkine in this post:
https://www.experts-exchange.com/questions/21258976/KeyPress-Help.html
~IM
ASKER
Dhaest - Thank you for responding. The article was interesting and will prove useful in the future but is a little overkill for what I need right now.
Planocz - Thanks for the code, I had tried something similar with similar results. Using your code (which I did) I should see the message "<CTRL> + N Captured", but I don't.
Originally I thought there was a simple solution so I only assigned 125 points. The solution seems more involved so I'm raising it to 250.
Thanks
Ken
Planocz - Thanks for the code, I had tried something similar with similar results. Using your code (which I did) I should see the message "<CTRL> + N Captured", but I don't.
Originally I thought there was a simple solution so I only assigned 125 points. The solution seems more involved so I'm raising it to 250.
Thanks
Ken
when you press the crtl N keys in a form sample it will chow up in the Form Title Header( on top of form)
This is to just show you that you captured those keys.
This is to just show you that you captured those keys.
ASKER
This was my attempt. I works if i press ctrl and then n, but not if I press both keys simultaneously.
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEv entArgs) Handles MyBase.KeyDown
If e.Control Then
Me.CtlKeyActive = True
Exit Sub
End If
If Me.CtlKeyActive = False Then
Exit Sub
End If
If e.KeyData.ToString = "N" Then ' Or e.Shift Then
MsgBox("UBH -CTRL-N")
End If
Me.CtlKeyActive = False
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEv
If e.Control Then
Me.CtlKeyActive = True
Exit Sub
End If
If Me.CtlKeyActive = False Then
Exit Sub
End If
If e.KeyData.ToString = "N" Then ' Or e.Shift Then
MsgBox("UBH -CTRL-N")
End If
Me.CtlKeyActive = False
End Sub
why do you need to press at same time. Most users can not do that correctly.
ASKER
Sorry planocz, what I meant was that while holding the ctrl key down the user would then press the N key, similar to typeing an uppercase n, you hold down the shift key and press the n key.
ASKER
Let me give you all some background because I might be approaching the core problem all wrong. I have a grid where I want the user to be able to change the value of a cell to "Null". Since they can't type in a 0 (this is an integer type column), i thought I would give them the option of highlighting the cell and pressing Ctrl-N.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://www.codeproject.com/vb/net/mclhotkeynet.asp