[RESOLVED] [2008] System freeze when trying to play a sub using a key (low keyboard hook)
Hi guys,
I have got a problem when trying to play a sub using a shortcut
key in a low keyboard hook...
Code:
Private Shadows Sub KeyDown(ByVal KeyCode As Integer) Handles KeyHook.KeyDown
If KeyCode=keys.f12 then
Call MySub
End If
when trying to play any of these subs by the key f12 the system freeze for a couple of seconds and the goes back to normal...
when clicking on a button that Calls the same Sub the system doesn't freeze...
The sub it calls is a loop with delay (system.threading.thread.sleep(10) )
and the Application.doevents command.
How can I fix the problem, so when I press the F12 key my system wont freeze (it doesn't happen to me when trying to call the same sub using a button)...?
Thank you!
Re: [2008] System freeze when trying to play a sub using a key (low keyboard hook)
how did you implement they keyboard hook?
Re: [2008] System freeze when trying to play a sub using a key (low keyboard hook)
here is the guide (someone that really really helped me from this forum, I'm really sorry for not remembering his name!) gave me this guide:
http://ih4x.wordpress.com/2008/05/05...d-hook-global/
Re: [2008] System freeze when trying to play a sub using a key (low keyboard hook)
The F12 key is reserved for use by the debugger at all times. Check out this link.
Re: [2008] System freeze when trying to play a sub using a key (low keyboard hook)
I have changed the key (for example to 1 (number key) ) and it still doesn't work...
any ideas?
Re: [2008] System freeze when trying to play a sub using a key (low keyboard hook)
What happens when you do..
Code:
Private Shadows Sub KeyDown(ByVal KeyCode As Integer) Handles KeyHook.KeyDown
If KeyCode=keys.D1 then
MyButton.PerformClick
End If
??
(substitute button name for 'MyButton', of course :))
Re: [2008] System freeze when trying to play a sub using a key (low keyboard hook)
Quote:
Originally Posted by shynet
Hi,
I've readed the link and found this:
This will basically convert the key that was pressed to its char equivalent, and then save it to a log file, however this isn’t suitable for all the keys (E.g. space, ctrl, alt, shift, etc) as they do not have char characters.
That could mean that the key your trying isn't suitable.
You can test it to try the key in the example.
Wkr,
sparrow1
Re: [2008] System freeze when trying to play a sub using a key (low keyboard hook)
Sorry for the late respond,
I have tried the Keys Space, Delete, or just P key to check everything and the system still kind of stucks when pressing it (Mouse can be moved but I can't do anything except moving it)
and I have tried what michaelerice said but it still doesn't work...
any more ideas?
Re: [2008] System freeze when trying to play a sub using a key (low keyboard hook)
did you try changing the routine that actually gets calls to not do any thread sleeping, and see if there is any different result?
Re: [2008] System freeze when trying to play a sub using a key (low keyboard hook)
Since clicking the button doesn't cause you grief, but PerformClick() on the same button does, you may be having a cross-threading issue that isn't apparent. Try this...
Code:
Delegate Sub InvokeDelegate()
...
Private Shadows Sub KeyDown(ByVal KeyCode As Integer) Handles KeyHook.KeyDown
If KeyCode=keys.D1 then
Me.BeginInvoke(New InvokeDelegate(AddressOf MyButton.PerformClick))
End If
Re: [2008] System freeze when trying to play a sub using a key (low keyboard hook)
Quote:
Originally Posted by michaelerice
Since clicking the button doesn't cause you grief, but PerformClick() on the same button does, you may be having a cross-threading issue that isn't apparent. Try this...
Code:
Delegate Sub InvokeDelegate()
...
Private Shadows Sub KeyDown(ByVal KeyCode As Integer) Handles KeyHook.KeyDown
If KeyCode=keys.D1 then
Me.BeginInvoke(New InvokeDelegate(AddressOf MyButton.PerformClick))
End If
Thanks!!! that really worked!
I read some info about the Delegate in vb .net , and I want to understand why this happend and what cross threading issue means?
I will read more about the delegate, but what the cross threading issue means?
Thanks!!
Re: [2008] System freeze when trying to play a sub using a key (low keyboard hook)
Here is a link that can probably explain it better than I:
http://msdn.microsoft.com/en-us/library/ms171728.aspx
Re: [2008] System freeze when trying to play a sub using a key (low keyboard hook)
Thanks again, but now I noticed that other keys I press doesn't log anymore
after pressing the key I assigned to the delegate. how can I make the keys log again after the key I assigned was pressed?
EDIT:
Just moved the code to the bottom of the sub so it won't "smash" other commands after it,
thank you very much!