|
-
Oct 20th, 2010, 06:41 AM
#1
Thread Starter
New Member
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
-
Oct 20th, 2010, 09:30 AM
#2
Re: How can i detect what key has been pressed on a form that has command buttons on
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Oct 20th, 2010, 11:32 AM
#3
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.
Last edited by Ellis Dee; Oct 20th, 2010 at 11:37 AM.
-
Oct 20th, 2010, 12:22 PM
#4
Thread Starter
New Member
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?
-
Oct 20th, 2010, 12:31 PM
#5
Re: How can i detect what key has been pressed on a form that has command buttons on
-
Oct 20th, 2010, 12:47 PM
#6
Thread Starter
New Member
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?
-
Oct 20th, 2010, 09:23 PM
#7
Re: How can i detect what key has been pressed on a form that has command buttons on
 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 
I was thinking about hiding that textbox to somewhere else, outside the user's focus. Eg: Text1.Left=-9999
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Oct 22nd, 2010, 03:01 AM
#8
New Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|