VB School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

How to add shortcut keys in Visual Basic Example, I wanted the compute button's shortcut on keyboard as F4 Rate Topic: -----

#4 marckiestar  Icon User is offline

  • New D.I.C Head
  • Pip

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


Dream Kudos: 0

Re: How to add shortcut keys in Visual Basic

Post icon  Posted 29 September 2008 - 06: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
  • Icon

Reputation: 103
  • Posts: 1,223
  • Joined: 17-April 07


Dream Kudos: 150

Re: How to add shortcut keys in Visual Basic

Posted 29 September 2008 - 06: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 - 08:54 PM

Was This Post Helpful? 0
  • +
  • -

#6 akhileshbc  Icon User is offline

  • D.I.C Head
  • Icon

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


Dream Kudos: 50

Re: How to add shortcut keys in Visual Basic

Posted 30 September 2008 - 04: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
  • Icon

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


Dream Kudos: 50

Re: How to add shortcut keys in Visual Basic

Posted 03 October 2008 - 06: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
  • Icon

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


Dream Kudos: 50

Re: How to add shortcut keys in Visual Basic

Posted 03 October 2008 - 06: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
  • Icon

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


Dream Kudos: 50

Re: How to add shortcut keys in Visual Basic

Posted 03 October 2008 - 07: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
  • Icon

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


Dream Kudos: 50

Re: How to add shortcut keys in Visual Basic

Posted 03 October 2008 - 07:13 AM

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

#11 reyben_89  Icon User is offline

  • D.I.C Head
  • PipPip

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


Dream Kudos: 0

Re: How to add shortcut keys in Visual Basic

Posted 30 December 2008 - 02:52 AM

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



Fast Reply

  

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users