Click to See Complete Forum and Search --> : Need help with KeyPress


Junday
June 7th, 2009, 03:38 PM
Hello.

I am making a macro-program for a friend, that should let him bind the F-keys from F1 to F12 to enter a text but i got a little problem. The only problem is that i need to find (a) correct line(s).

I have tried with KeyChar and i have readed about that you should use ascii-codes like numbers for each key. But i have tried the codes with no luck.

I'll hope you could help me:

If my friend press F2 it should do an action (it could be a msgbox say "Yay") and if he press F1 it do another action.

If this is not information enough, just ask i will upload a image so you can see what i mean.

- Thanks (Thanks DataMiser for your help in my other topic - i did'nt solve it but thanks anyway)

ComITSolutions
June 8th, 2009, 01:15 AM
If you are trying to find which key is pressed. Your code should be in keydown event not in keypress

dglienna
June 8th, 2009, 10:34 PM
You can't override some windows commands, such as ALT-F4 to close a window, or F1 for help (in most apps), but you can trap them from within your own app

Junday
June 9th, 2009, 10:57 AM
Ok, now i find the syntax - Thanks to my friend that helped me by google it look like:

Private Sub Form1_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles Me.KeyDown
If (e.KeyCode = Keys.F5) Then Timer1.Enabled = True
e.Handled = True
End Sub

It work great.... Until i saw that it only worked INSIDE the form :(...

Now i need to have it work outside the form, and i readed that you should have a keyboard hook... But i don't know how that should work together with this project, or are there any syntax/API for that?

Thanks for your help, great forum to get help :)

- is it then possible to get help with this? Thank you :o)

dglienna
June 9th, 2009, 07:01 PM
A keyboard hook is used to hook WINDOWS messages, and attempt to intercept them. You still can't trap F1, like I said.

Because you can trap keys with this method, we can't really discuss it here.