I'm trying to make my program execute "stuff" when I press a key such as F1 (or any other key I choose) I want to do this WITHOUT shortcuts, so that i can later do it without having an object heres what I have so far
VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'adds the handler and sets address only works when it's in the text box
AddHandler txtmacro.KeyPress, AddressOf key
End Sub
Public Sub key(ByVal o As System.Object, ByVal e As KeyPressEventArgs)
'Checks to see when the letter "a" is pressed and then does "stuff"
If e.KeyChar = Microsoft.VisualBasic.ChrW(97) Then
Stuff
End If
End Sub
Now this works fine under 2 condittions
1)I'm typing in my text box
2)I use a key that has a common ascii code such as a-z A-Z or 0-9
When i put in "&H6F" for the key (which I thought was the F1 key) It doesnt give me any errors but it doesnt do the "stuff"
Also I would like for this to work at any time, and not just when the cursor is in the text box.
Any help or suggestions would be greatly appreciated
even if you have a completely differant way of doing this, as long as it works that's great.
Do you want your form to trap all keys first? even if you are in a Textbox or other control? If so you should set KeyPreview of your form to True, then:
VB Code:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.F1 Then
' Do the stuff here
End If
e.Handled = True
End Sub
Last edited by Lunatic3; Feb 14th, 2004 at 12:44 AM.
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
I attached a sample that involves creating a user control that inherits from a textbox
it provides two new events SpecialKeyUp and SpecialKeyDown
these events will fire when the control receives a WM_KEYUP, WM_KEYDOWN, WM_SYSKEYUP, WM_SYSKEYDOWN
Both events are declared like this
byval msg as Message, byval key as Keys
msg will be WM_KEYUP, WM_KEYDOWN, WM_SYSKEYUP or WM_SYSKEYDOWN
key will be the key that was pressed
Last edited by ZeroCool; Feb 14th, 2004 at 12:46 AM.
for my example you need to download the attachment that i posted that will provide you with a simple control that inherits from the textbox, for a begginer you might be better off with Lunatic3's example.
VB Code:
'Same thing with my control --->
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load