How to trigger a macro with a letter key without "Ctrl"

john12345

New Member
Joined
Apr 6, 2016
Messages
9
Hello, :)

So the problem is, I am creating a simple game in Excel&VBA with two players, each player needs to control four keys. For the first player, I used the four direction keys and Application.OnKey function, like this:

Sub DeclareKeys()
Application.OnKey "{UP}", "Move_up"
Application.OnKey "{DOWN}", "Move_down"
Application.OnKey "{LEFT}", "Move_left"
Application.OnKey "{RIGHT}", "Move_right"
End Sub

But for the second player, I have to use those letter keys like "w, s, a, d" to trigger macros, but I cannot use Application.OnKey for those keys. I can also assign shortcut using "Ctrl + w", "Ctrl + d" ..., but that is not user-friendly for the player, and also the input from the direction keys combining the "Ctrl" will have some other negative effects.

So basically, how to trigger a macro with only a letter key, without "Ctrl" or anything else?

Thank you so much!
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
I haven't played around with what you are doing but after short research I came up with this:

For key "a" to trigger a macro without holding CTRL type:
Code:
[COLOR=#333333]Application.OnKey "a", "some_proc"

it seems {} is needed for special keys like UP, Down or Enter.

Nard[/COLOR]
 
Upvote 0
Hello again,

I tried Application.OnKey "a", "some_proc"

But it does not work. When I click one the keyboard, it just makes what I type appear somewhere on the datasheet, and the Macro cannot be triggered.

I am starting to doubt if it is possible or not.

Anyone knows the answer? THANKS!
 
Upvote 0
Hi John

Use the key ascii code.

For ex., for the letter "A", the ascii code is 65, try:

Application.OnKey "{65}", "TestA"
 
Upvote 0
I'm glad it helped.

Remark: it should also work with the letter

Code:
Application.OnKey "{a}", "TestA"
 
Upvote 0

Forum statistics

Threads
1,215,263
Messages
6,123,958
Members
449,135
Latest member
jcschafer209

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top