How to add shortcut keys in Visual Basic

Example, I wanted the compute button's shortcut on keyboard as F4

Page 1 of 1

11 Replies - 44294 Views - Last Post: 17 March 2013 - 11:02 PM Rate Topic: -----

#4 marckiestar  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 21
  • Joined: 21-August 07

Re: How to add shortcut keys in Visual Basic

Post icon  Posted 29 September 2008 - 07:08 PM

Hi does anyone here knows the code on how to put shorcut keys?

For example:

I have my program, it has 3 buttons, Compute, Clear, and Exit.

Just in the case that there is no mouse, the user could press keys on the keyboard instead of pointing the mouse into the button then click.

Just like this, Compute - > F4
Clear - > F5
Exit - > F6

How will I assign those keys to link them with the buttons?

Can you give an example sourcecode for it?

Thanks!
Was This Post Helpful? 1

#5 thava  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,606
  • Joined: 17-April 07

Re: How to add shortcut keys in Visual Basic

Posted 29 September 2008 - 07:13 PM

there is no special property for this

if Vb6
use & char in your caption

for example &Compute will display like this Compute
this means ALT+C will invoke the button click

'sorry i make mistake here its not property its event
if you still want the function keys use forms keydown Event
there
place the code

if keycode = vbkeyf4 then
cmdcompute_click()
End if

one small thing this code will not execute if you not set form's keypreview property to true

This post has been edited by thava: 29 September 2008 - 09:54 PM

Was This Post Helpful? 1
  • +
  • -

#6 akhileshbc  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 179
  • Joined: 27-September 08

Re: How to add shortcut keys in Visual Basic

Posted 30 September 2008 - 05:30 AM

Or create some menu using menu editor and assign shortcut keys to it(also make its visible property to false), then write:
private sub mnuCompute_click()
cmdCompute_click
end sub
Was This Post Helpful? 0
  • +
  • -

#7 Reverand Dave  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • Posts: 375
  • Joined: 27-July 08

Re: How to add shortcut keys in Visual Basic

Posted 03 October 2008 - 07:21 AM

Another way to do this would be to tie the events to the OnKey or KeyPress event. This would obviate the need to add extra features like a menu or what not. Just have the events run like this


Sub Form_KeyPress(KeyAscii As Integer) 'Can substitue form with any other control that supports this event

If keycode = vbkeyf4 then 
	 Compute 'add your computing subroutine here
Elseif KeyCode =  vbkeyf5 then 
	 Clear  'add your clearing subroutine here
Elseif KeyCode = vbkeyf6 then
	 Exit  'add your exit subroutine here
end if



Was This Post Helpful? 0
  • +
  • -

#8 akhileshbc  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 179
  • Joined: 27-September 08

Re: How to add shortcut keys in Visual Basic

Posted 03 October 2008 - 07:55 AM

@dave, ur code seems to have some mistakes. U r using the keypress event. It only returns the keyascii. And u r using keycode in ur code. Ur code has to b inside the keydown event like thava had told us in the earlier post. :)
Was This Post Helpful? 0
  • +
  • -

#9 Reverand Dave  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • Posts: 375
  • Joined: 27-July 08

Re: How to add shortcut keys in Visual Basic

Posted 03 October 2008 - 08:06 AM

My mistake. You'll want to use this code then:

Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) 'Can substitue form with any other control that supports this event
 
If KeyCode = 115 then '115 is the numeric KeyCode for F4
	 Compute 'add your computing subroutine here
Elseif KeyCode = 116 then '116 is the numeric KeyCode for F5
	 Clear  'add your clearing subroutine here
Elseif KeyCode = 117 then  '117 is the numeric KeyCode for F6
	 Exit  'add your exit subroutine here
end if


Was This Post Helpful? 0
  • +
  • -

#10 akhileshbc  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 179
  • Joined: 27-September 08

Re: How to add shortcut keys in Visual Basic

Posted 03 October 2008 - 08:13 AM

:) :) :D
Was This Post Helpful? 0
  • +
  • -

#11 reyben_89  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 69
  • Joined: 30-November 08

Re: How to add shortcut keys in Visual Basic

Posted 30 December 2008 - 03:52 AM

have i nice day to all this post is very usefull.
Was This Post Helpful? 0
  • +
  • -

#12 Erwin10452  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-May 08

Re: How to add shortcut keys in Visual Basic

Posted 16 March 2013 - 05:52 AM

Great info on this topic! Nice to know about the form's KeyPreview property as well since it has to be enabled for any of this to work. I'm still using VB6 in 2013 and these legacy articles are a big help.
Was This Post Helpful? 0
  • +
  • -

#13 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 572
  • View blog
  • Posts: 2,985
  • Joined: 19-May 09

Re: How to add shortcut keys in Visual Basic

Posted 17 March 2013 - 12:58 PM

View PostReverand Dave, on 03 October 2008 - 09:06 AM, said:

My mistake. You'll want to use this code then:

Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) 'Can substitue form with any other control that supports this event
 
If KeyCode = 115 then '115 is the numeric KeyCode for F4
	 Compute 'add your computing subroutine here
Elseif KeyCode = 116 then '116 is the numeric KeyCode for F5
	 Clear  'add your clearing subroutine here
Elseif KeyCode = 117 then  '117 is the numeric KeyCode for F6
	 Exit  'add your exit subroutine here
end if


Dave, a combination of your post and thava's gives a working answer. As thava says, the form's KeyPreview property must be set to true, or the key will be sent to the current control first. Second, vbKeyF4 = 115, for example, and the constants are easier to remember. Finally, select case is more efficient in this situation than if then elseif:
Select Case KeyCode
    Case vbKeyF4
        'Compute
    Case vbKeyF5
        'Clear
    Case vbKeyF6
        'Exit
End Select

The reason it's more efficient is because you only reference the KeyCode variable once instead of once for each condition.
Case vbKeyF6
Was This Post Helpful? 0
  • +
  • -

#14 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3177
  • View blog
  • Posts: 10,638
  • Joined: 12-December 12

Re: How to add shortcut keys in Visual Basic

Posted 17 March 2013 - 01:03 PM

@Bob Check the date :) but, nevertheless, still useful information!

This post has been edited by andrewsw: 17 March 2013 - 01:04 PM

Was This Post Helpful? 0
  • +
  • -

#15 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 572
  • View blog
  • Posts: 2,985
  • Joined: 19-May 09

Re: How to add shortcut keys in Visual Basic

Posted 17 March 2013 - 11:02 PM

LOL I hate it when someone necroposts and I jump on the bandwagon.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1