How can i detect what key has been pressed on a form that has command buttons on it?
How can i detect what key has been pressed on a form that has command
buttons on it? I know that using the Form_KeyDown event with the
Form.KeyPreview property et to True works when there are no buttons on
the form, but i have buttons on the form and when i try to detect
events like pressing the arrow keys, the program doe...s not respond to
the events but instead just shifts focus from button to button using
the arrow keys.
When the focus is on the command button it detects most of the keyboard inputs except for the arrow keys. Help me, please!!
Link:
http://www.mediafire.com/file/47jfve...entKeyDown.rar
src:
Private Sub Command1_KeyDown(KeyCode As Integer, Shift As Integer)
'SendKeys (vbKeyShift)
If KeyCode = 40 Then
MsgBox ("Key DOWN")
End If
If KeyCode = 38 Then
MsgBox ("Key UP")
End If
End Sub
Private Sub Form_Load()
KeyPreview = True
End Sub
Re: How can i detect what key has been pressed on a form that has command buttons on
Hi quangvinh777... Welcome to the forums...:wave:
You must write the code in the Form's KeyDown event !
I think, a workaround for the focus problem of command button is to set focus to some other control other than buttons (eg: TextBox).
Maybe, someone will post a better solution :)
Code:
Option Explicit
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 40 Then
MsgBox ("Key DOWN")
End If
If KeyCode = 38 Then
MsgBox ("Key UP")
End If
End Sub
Private Sub Form_Load()
KeyPreview = True
Text1.TabIndex = 0 '~~~ You have to create a Textbox and set TabIndex of it as first one.
End Sub
:wave:
Re: How can i detect what key has been pressed on a form that has command buttons on
I don't think you can trap the arrow keys (or the tab key) using native VB. Maybe if you subclassed?
My standard workaround is to rethink my GUI design. If I really absolutely must implement custom arrow navigation, I use the number pad. (Have "4" do the desired left arrow action, "8" does up, et cetera...)
EDIT: akhileshbc, pictureboxes are better for hiding focus. They have the advantage of not displaying a blinking caret.
EDIT2: I just had a thought: if you set every control's TabStop property to False, you might be able to trap arrow keys manually in the Key events since they wouldn't be initiating any navigation. You'd then need to manually handle tabbing, but at least it's worth a shot.
Re: How can i detect what key has been pressed on a form that has command buttons on
@akhileshbc, @Ellis Dee: Thanks!
ex:
vb Code:
Private Sub Command1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 40 Then
Command2_click
End If
End Sub
Private Sub Command2_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 38 Then
Command1_click
End If
If KeyCode = 40 Then
Command3_click
End IF
End Sub
...
How can i do?
Re: How can i detect what key has been pressed on a form that has command buttons on
Re: How can i detect what key has been pressed on a form that has command buttons on
what should I do to fulfill the request above?
Re: How can i detect what key has been pressed on a form that has command buttons on
Quote:
Originally Posted by
Ellis Dee
EDIT: akhileshbc, pictureboxes are better for hiding focus. They have the advantage of not displaying a blinking caret.
EDIT2: I just had a thought: if you set every control's TabStop property to False, you might be able to trap arrow keys manually in the Key events since they wouldn't be initiating any navigation. You'd then need to manually handle tabbing, but at least it's worth a shot.
Great idea, Ellis :thumb:
I was thinking about hiding that textbox to somewhere else, outside the user's focus. Eg: Text1.Left=-9999 :)
Re: How can i detect what key has been pressed on a form that has command buttons on
If you've set KeyPreview = True on the form, and you toggle NumLock on the keyboard keypad you should be able to trap any keystroke, including CTRL and ALT in KeyDown and/or KeyPress.
You may have to TELL your user to toggle NumLock, unless you want to force that on them. And of course, you'll have to tell them to use the Arrow Keys from the KEYPAD.
I'm new here, I hope I haven't misunderstood the problem.
Good luck.