[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 525
  • Last Modified:

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.
0
KenDickinson
Asked:
KenDickinson
1 Solution
 
DhaestCommented:
0
 
planoczCommented:
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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Another hotkey example but it shows how to deal with multiple hotkeys:
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21102359.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:
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21258976.html

~IM
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
KenDickinsonAuthor Commented:
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
0
 
planoczCommented:
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.
0
 
KenDickinsonAuthor Commented:
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.KeyEventArgs) 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
0
 
planoczCommented:
why do you need to press at same time. Most users can not do that correctly.
0
 
KenDickinsonAuthor Commented:
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.
0
 
KenDickinsonAuthor Commented:
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.
0
 
planoczCommented:
That is what I use in my Datagrid....
SAMPLE for cols 1 and 2 ...

  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

        Try
            If ((msg.Msg = WM_KEYDOWN) Or (msg.Msg = WM_SYSKEYDOWN)) Then
                Select Case (keyData)
                    Case Keys.Enter
                        SendKeys.Send("{TAB}")
                    Case Keys.F9
                        'txtQty.Text = datagrid1.Item(datagrid1.CurrentRowIndex, 2)
                        'txtDescription.Text = datagrid1.Item(datagrid1.CurrentRowIndex, 1)
                        datagrid1.Item(datagrid1.CurrentRowIndex, 2) = txtQty.Text
                    Case Keys.Right, Keys.Left, Keys.Down, Keys.Up, Keys.Tab
                        txtQty.Text = datagrid1.Item(datagrid1.CurrentRowIndex, 2)
                        txtDescription.Text = datagrid1.Item(datagrid1.CurrentRowIndex, 1)
                    Case Keys.A To Keys.Z
                    Case Keys.NumPad0 To Keys.NumPad9, Keys.D0 To Keys.D9
                    Case (Keys.Control Or Keys.N)
                              datagrid1.Item(datagrid1.CurrentRowIndex, 2) = ""   '<--- NULL
                    Case (Keys.Alt Or Keys.Z)
                        '  Me.Parent.Text = "<ALT> + Z Captured"
                End Select
            End If
        Catch ex As InvalidCastException
            'Skip DBNULL
        Catch ex As SystemException
            MsgBox(ex.StackTrace & ex.Message, MsgBoxStyle.Critical, "General Error")
        End Try
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now