Results 1 to 16 of 16

Thread: (Resolved) How to detect arrow and shift keys in vb.net

  1. #1

    Thread Starter
    Addicted Member Sanko's Avatar
    Join Date
    Mar 2000
    Location
    Argentina
    Posts
    128

    Resolved (Resolved) How to detect arrow and shift keys in vb.net

    Hi i have this problem developing a custom control.

    1) I need to detect if an up, down, left or right arrow is pressed.

    2) And also i need to detect if i have pressed Shift+ Up or Shift + Left or Shift + Right or Shift +Down

    1) Only detect 1 key pressed
    2) Detect 2 keys Shif + Arrow pressed

    So i search and found this to detect the arrows keys
    VB Code:
    1. Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
    2.         Const WM_KEYDOWN As Integer = &H100
    3.         Const WM_SYSKEYDOWN As Integer = &H104
    4.         If ((msg.Msg = WM_KEYDOWN) Or (msg.Msg = WM_SYSKEYDOWN)) Then
    5.             Select Case (keyData)
    6.                   Case Keys.Left
    7.                        msgbox ("L")
    8.                   Case Keys.Right
    9.                        msgbox ("R")
    10.                   Case Keys.Up
    11.                        msgbox ("U")
    12.                   Case Keys.Down
    13.                        msgbox ("D")
    14.                   Case Keys.Shift
    15.                        msgbox ("S")
    16.                   Case Keys.Control
    17.                        msgbox ("C")
    18. Return MyBase.ProcessCmdKey(msg, keyData)
    19. End Function
    This work only for the arrows keys because if i press Shift or Ctrl keys dont show the message box
    So i change for this
    VB Code:
    1. ' Normally you can handle Key Down Event to trap Keys
    2.     Private Sub AreaTrabajo_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    3.         MsgBox(e.KeyData.ToString & " pressed. Key Value is ", MsgBoxStyle.Information, "KeyDown Handler")
    4.     End Sub
    If i press the Shift or Control is ok but if i press and arrow key it not work.

    So how i can detect the Arrow and Shift keys pressed together?
    Last edited by Sanko; Jun 2nd, 2006 at 02:10 PM. Reason: Resolved

  2. #2
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: How to detect arrow and shift keys in vb.net

    This is a modified version of the mentalis API code:
    VB Code:
    1. #Region "Keyboard Hook"
    2.     Private intHook As Int32
    3.     Private Delegate Function KeyboardHookDelegate(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
    4.     <System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.FunctionPtr)> Private callback As KeyboardHookDelegate
    5.  
    6.     Private Structure KBDLLHOOKSTRUCT
    7.         Friend vkCode As Integer
    8.         Friend scanCode As Integer
    9.         Friend flags As Integer
    10.         Friend time As Integer
    11.         Friend dwExtraInfo As Integer
    12.     End Structure
    13.  
    14.     Private Declare Function SetWindowsHookEx Lib "user32.dll" Alias "SetWindowsHookExA" (ByVal idHook As Int32, ByVal lpfn As KeyboardHookDelegate, ByVal hmod As Int32, ByVal dwThreadId As Int32) As Int32
    15.     Private Declare Function GetAsyncKeyState Lib "user32.dll" Alias "GetAsyncKeyState" (ByVal vKey As Int32) As Int32
    16.     Private Declare Function UnhookWindowsHookEx Lib "user32.dll" Alias "UnhookWindowsHookEx" (ByVal hHook As Int32) As Int32
    17.  
    18.     Private Sub HookKeyboard(ByVal blnOn As Boolean)
    19.         Select Case blnOn
    20.             Case True
    21.                 callback = New KeyboardHookDelegate(AddressOf KeyboardCallback)
    22.                 intHook = SetWindowsHookEx(13&, callback, System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.  GetModules()(0)).ToInt32, &H0)
    23.             Case False
    24.                 UnhookWindowsHookEx(intHook)
    25.         End Select
    26.     End Sub
    27.  
    28.     Private intCtrl As Int32 = &H11
    29.     Private intShft As Int32 = &H10
    30.     Private intA As Int32 = &H41
    31.     Private Function KeyboardCallback(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
    32.         If CBool(GetAsyncKeyState(intCtrl)) And CBool(GetAsyncKeyState(intShft)) And CBool(GetAsyncKeyState(intA)) Then
    33.             Me.Show()
    34.         End If
    35.     End Function
    36. #End Region
    It watches for Ctrl+Shift+A and then displays itself (I used it in a windows form). All you'd need to do is modify it a little (change the constants or whatever) and it should work for you.

  3. #3
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: How to detect arrow and shift keys in vb.net

    Then to set the hooks:
    VB Code:
    1. Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Me.HookKeyboard(True)
    3.     End Sub
    4.  
    5.     Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    6.         Me.HookKeyboard(False)
    7.     End Sub

    I should also add, this is a global hook.
    Last edited by sevenhalo; Jun 2nd, 2006 at 11:08 AM.

  4. #4
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: How to detect arrow and shift keys in vb.net

    I am not sure what version of Visual Studio are you using, but theis works for 2005.
    VB Code:
    1. Private Sub YouControl_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles YouControl.KeyDown
    2.  
    3.   If e.Shift And e.KeyValue = Keys.Up Then
    4.  
    5.   'you code goes here
    6.  
    7.   End If
    8.  
    9. End Sub
    Last edited by VBDT; Jun 2nd, 2006 at 12:36 PM.

  5. #5
    New Member
    Join Date
    May 2006
    Posts
    13

    Re: How to detect arrow and shift keys in vb.net

    thanks for the link

  6. #6
    New Member
    Join Date
    May 2006
    Posts
    13

    Re: How to detect arrow and shift keys in vb.net

    so its somthing that links two programs? if i got that right?

  7. #7

    Thread Starter
    Addicted Member Sanko's Avatar
    Join Date
    Mar 2000
    Location
    Argentina
    Posts
    128

    Re: How to detect arrow and shift keys in vb.net

    Quote Originally Posted by VBDT
    I am not sure what version of Visual Studio are you using, but theis works for 2005.
    VB Code:
    1. Private Sub YouControl_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles YouControl.KeyDown  
    2.   If e.Shift And e.KeyValue = Keys.Up Then
    3.   'you code goes here
    4.   End If
    5. End Sub
    Hi i try your code but there is an error because the Keydown event belongs to MyBase so in a custom control it would be
    VB Code:
    1. ' Normally you can handle Key Down Event to trap Keys
    2.     Private Sub YourControl_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    3.         MsgBox(e.KeyData.ToString & " pressed. Key Value is ", MsgBoxStyle.Information, "KeyDown Handler")
    4.     End Sub
    And yes in my first post i already try this but somehow it not detect the arrows (keys.up, keys.down, etc...) Maybe is a VS.2005 Bug.

    If you try in a form it works but in a custom control no

  8. #8

    Thread Starter
    Addicted Member Sanko's Avatar
    Join Date
    Mar 2000
    Location
    Argentina
    Posts
    128

    Smile Re: How to detect arrow and shift keys in vb.net

    Quote Originally Posted by sevenhalo
    Then to set the hooks:
    VB Code:
    1. Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Me.HookKeyboard(True)
    3.     End Sub
    4.  
    5.     Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    6.         Me.HookKeyboard(False)
    7.     End Sub
    I should also add, this is a global hook.
    Yes finaly it work with this hook.

    Q: When using a hook: this can reduce the system performance of the app, or use more memory

  9. #9
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: (Resolved) How to detect arrow and shift keys in vb.net

    Umm, not quite understanding your question... So I'll go with more memory.

    I think since you're using the API call to place the hook, it's out of your hands and only receiving messages when they're made. Your program is on a seperated thread, so it shouldn't be bothered.

    (Don't quote me on that, but it's what I beleive to be happening)

  10. #10
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: How to detect arrow and shift keys in vb.net

    Quote Originally Posted by Sanko
    Hi i try your code but there is an error because the Keydown event belongs to MyBase so in a custom control it would be
    VB Code:
    1. ' Normally you can handle Key Down Event to trap Keys
    2.     Private Sub YourControl_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    3.         MsgBox(e.KeyData.ToString & " pressed. Key Value is ", MsgBoxStyle.Information, "KeyDown Handler")
    4.     End Sub
    And yes in my first post i already try this but somehow it not detect the arrows (keys.up, keys.down, etc...) Maybe is a VS.2005 Bug.

    If you try in a form it works but in a custom control no
    In my example the important part is not how you handle the event but what goes in the event handler!
    VB Code:
    1. If e.Shift And e.KeyValue = Keys.Up Then
    2.   'you code goes here
    3. End If
    If you have access to e.Shift And e.KeyValue then there should be no problems!
    Last edited by VBDT; Jun 2nd, 2006 at 02:56 PM.

  11. #11

    Thread Starter
    Addicted Member Sanko's Avatar
    Join Date
    Mar 2000
    Location
    Argentina
    Posts
    128

    Re: How to detect arrow and shift keys in vb.net

    Quote Originally Posted by VBDT
    If you have access to e.Shift And e.KeyValue then there should be no problems!
    My control name is AreaTrabajo.
    If i try this code in the control class
    VB Code:
    1. Private Sub AreaTrabajo_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    2.         If e.Shift And e.KeyValue = Keys.Up Then
    3.             MsgBox("Shift and up pressed")
    4.         End If
    5.     End Sub
    And i press Shift + Up arrow key with my control focused nothing appens. It should show the message box "Shift and up pressed", but never show it....
    Bah i think maybe is a bug in .Net

  12. #12
    Hyperactive Member sheikh78's Avatar
    Join Date
    Apr 2006
    Location
    C:/
    Posts
    423

    Re: (Resolved) How to detect arrow and shift keys in vb.net

    Maybe you should try e.keycode?

    Example:

    VB Code:
    1. Private Sub AreaTrabajo_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    2.         If e.Shift And e.KeyCode = Keys.Up Then
    3.             MsgBox("Shift and up pressed")
    4.         End If
    5.     End Sub

    I think that should work. I am positive. Good Luck!
    "Imagination is more important than knowledge" - Albert Einstein, born on March 14th 1879.
    Can't find it here on VBForums? Go to the CodeProject. MSDN is your friend . I have such a bad website, my friend decided it would be funny to change the template and he moderates the site for me: visit my site!

    "Thinking of you, wherever you are
    We pray for our sorrows to end, and hope that our hearts will blend.
    Now I will step forward to realize this wish.
    And who knows, starting a new journey may not be so hard…
    Or maybe it has already begun.
    There are many worlds, but they share the same sky
    one sky, one destiny..."

  13. #13
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: (Resolved) How to detect arrow and shift keys in vb.net

    What do you mean nothing happens? Did you debug it? Does the event occur but nothing happens?
    Last edited by VBDT; Jun 2nd, 2006 at 08:56 PM.

  14. #14

    Thread Starter
    Addicted Member Sanko's Avatar
    Join Date
    Mar 2000
    Location
    Argentina
    Posts
    128

    Re: (Resolved) How to detect arrow and shift keys in vb.net

    Yes i was using Breakpoints, debug. Anyway if i press Shift + Up dont show the msgbox.

  15. #15

    Thread Starter
    Addicted Member Sanko's Avatar
    Join Date
    Mar 2000
    Location
    Argentina
    Posts
    128

    Re: (Resolved) How to detect arrow and shift keys in vb.net

    Here is the code, open the solution and run the form then press any arrow key no msgbox show y you press any other key the event fires.
    Attached Files Attached Files

  16. #16
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: (Resolved) How to detect arrow and shift keys in vb.net

    Ok, first of all the proper use for the custom control would be creating user control project, add it to your project which you did and drug and drop the user control from the toolbox on the form you need the control, which I did not see! Drugging and dropping the user control on to your form makes your job easier. You don’t need do write bunch of code.

    Now, from my tests I discovered that the user control behaves like a button control. In button control, “KeyDown” event does not fires if the arrow or the tab key is pressed, it just changes the focuses (you can check it). But if you add a textbox onto the custom control and make keyDown event in the custom control class then it will fire the event when the arrow or the tab keys pressed for the textbox keyDown event. Other then that any key down event fires on the custom control keyDown if the keys are not arrow or tab!
    VB Code:
    1. Public Class Form1
    2.  
    3.     Private Sub UserControl11_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles UserControl11.KeyDown
    4.  
    5.     End Sub
    6.  
    7.     Private Sub UserControl11_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UserControl11.Load
    8.  
    9.     End Sub
    10. End Class
    This is why I said that it would be better if you drug and drop the user control on to your form. You can handle the custom control events from your form!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width