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)
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: (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)
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!
"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..."
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!
Last edited by VBDT; Jun 3rd, 2006 at 02:41 PM.
Rating is a way of saying thank you. Don't forget to rate always!