(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:
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.Left
msgbox ("L")
Case Keys.Right
msgbox ("R")
Case Keys.Up
msgbox ("U")
Case Keys.Down
msgbox ("D")
Case Keys.Shift
msgbox ("S")
Case Keys.Control
msgbox ("C")
Return MyBase.ProcessCmdKey(msg, keyData)
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:
' Normally you can handle Key Down Event to trap Keys
Private Sub AreaTrabajo_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
MsgBox(e.KeyData.ToString & " pressed. Key Value is ", MsgBoxStyle.Information, "KeyDown Handler")
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?
Re: How to detect arrow and shift keys in vb.net
This is a modified version of the mentalis API code:
VB Code:
#Region "Keyboard Hook"
Private intHook As Int32
Private Delegate Function KeyboardHookDelegate(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
<System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.FunctionPtr)> Private callback As KeyboardHookDelegate
Private Structure KBDLLHOOKSTRUCT
Friend vkCode As Integer
Friend scanCode As Integer
Friend flags As Integer
Friend time As Integer
Friend dwExtraInfo As Integer
End Structure
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
Private Declare Function GetAsyncKeyState Lib "user32.dll" Alias "GetAsyncKeyState" (ByVal vKey As Int32) As Int32
Private Declare Function UnhookWindowsHookEx Lib "user32.dll" Alias "UnhookWindowsHookEx" (ByVal hHook As Int32) As Int32
Private Sub HookKeyboard(ByVal blnOn As Boolean)
Select Case blnOn
Case True
callback = New KeyboardHookDelegate(AddressOf KeyboardCallback)
intHook = SetWindowsHookEx(13&, callback, System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly. GetModules()(0)).ToInt32, &H0)
Case False
UnhookWindowsHookEx(intHook)
End Select
End Sub
Private intCtrl As Int32 = &H11
Private intShft As Int32 = &H10
Private intA As Int32 = &H41
Private Function KeyboardCallback(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
If CBool(GetAsyncKeyState(intCtrl)) And CBool(GetAsyncKeyState(intShft)) And CBool(GetAsyncKeyState(intA)) Then
Me.Show()
End If
End Function
#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.
Re: How to detect arrow and shift keys in vb.net
Then to set the hooks:
VB Code:
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.HookKeyboard(True)
End Sub
Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Me.HookKeyboard(False)
End Sub
I should also add, this is a global hook.
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:
Private Sub YouControl_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles YouControl.KeyDown
If e.Shift And e.KeyValue = Keys.Up Then
'you code goes here
End If
End Sub
Re: How to detect arrow and shift keys in vb.net
Re: How to detect arrow and shift keys in vb.net
so its somthing that links two programs? if i got that right?
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:
Private Sub YouControl_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles YouControl.KeyDown
If e.Shift And e.KeyValue = Keys.Up Then
'you code goes here
End If
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:
' Normally you can handle Key Down Event to trap Keys
Private Sub YourControl_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
MsgBox(e.KeyData.ToString & " pressed. Key Value is ", MsgBoxStyle.Information, "KeyDown Handler")
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 :eek:
Re: How to detect arrow and shift keys in vb.net
Quote:
Originally Posted by sevenhalo
Then to set the hooks:
VB Code:
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.HookKeyboard(True)
End Sub
Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Me.HookKeyboard(False)
End Sub
I should also add, this is a global hook.
:thumb: 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
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)
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:
' Normally you can handle Key Down Event to trap Keys
Private Sub YourControl_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
MsgBox(e.KeyData.ToString & " pressed. Key Value is ", MsgBoxStyle.Information, "KeyDown Handler")
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 :eek:
In my example the important part is not how you handle the event but what goes in the event handler!
VB Code:
If e.Shift And e.KeyValue = Keys.Up Then
'you code goes here
End If
If you have access to e.Shift And e.KeyValue then there should be no problems!
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:
Private Sub AreaTrabajo_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.Shift And e.KeyValue = Keys.Up Then
MsgBox("Shift and up pressed")
End If
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
Re: (Resolved) How to detect arrow and shift keys in vb.net
Maybe you should try e.keycode?
Example:
VB Code:
Private Sub AreaTrabajo_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.Shift And e.KeyCode = Keys.Up Then
MsgBox("Shift and up pressed")
End If
End Sub
I think that should work. I am positive. Good Luck! :)
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?
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.
1 Attachment(s)
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.
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:
Public Class Form1
Private Sub UserControl11_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles UserControl11.KeyDown
End Sub
Private Sub UserControl11_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UserControl11.Load
End Sub
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!