PDA

View Full Version : [VBA] How to trigger a macro with a letter key without "Ctrl"



johnli12345
04-07-2016, 01:51 AM
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?


By the way, if we remove the {} like this: Application.OnKey "a", "some_proc" , it did not work, and when I click one the keyboard, it just makes what I type appear somewhere on the datasheet, and the Macro cannot be triggered.


Thank you so much!

johnli12345
04-07-2016, 02:12 AM
Problem solved, using ASCII code would do.