Application.onkey not working?

Elogger

New Member
Joined
Oct 22, 2015
Messages
23
Hi,

I am trying to use the WASD keys to move the current cell up,down,left and right however my application.onkey is not working.

Code:
Public Sub workbook_open(ByVal target As Range)Application.OnKey ("w"), "moveup"
Application.OnKey ("a"), "moveleft"
Application.OnKey ("s"), "moveright"
Application.OnKey ("d"), "movedown"
End Sub

This is what I have in my workbook module and then in a seperate module I have the offset functions. However when I press W a W appears in the cell.

Anyone know what I am doing wrong?

Thanks
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
It should work unless you are editing the cell; while you are editing the cell that action takes precedence and the macro won't run.

Another possibility is that macro security is set to disable macros without notification.
 
Last edited:
Upvote 0
It should work unless you are editing the cell; while you are editing the cell that action takes precedence and the macro won't run.

Another possibility is that macro security is set to disable macros without notification.

Macro security is set to enable but it still doesn't work. How do I make it that when I press W or A or any of the keys that excel doesn't automatically begin editing the cell?
 
Upvote 0
Hi,

I am trying to use the WASD keys to move the current cell up,down,left and right however my application.onkey is not working.

Code:
Public Sub workbook_open(ByVal target As Range)Application.OnKey ("w"), "moveup"
Application.OnKey ("a"), "moveleft"
Application.OnKey ("s"), "moveright"
Application.OnKey ("d"), "movedown"
End Sub

This is what I have in my workbook module and then in a seperate module I have the offset functions. However when I press W a W appears in the cell.

Anyone know what I am doing wrong?

Thanks

Hi,

I think it's because your macro never got activated, I think it's in the wrong place.

Try this:

Copy the following codes in the ThisWorkbook module:

Code:
Private Sub Workbook_Activate()
With Application
    .OnKey ("w"), "moveup"
    .OnKey ("a"), "moveleft"
    .OnKey ("s"), "moveright"
    .OnKey ("d"), "movedown"
End With
End Sub

Private Sub Workbook_Deactivate()
With Application
    .OnKey ("w")
    .OnKey ("a")
    .OnKey ("s")
    .OnKey ("d")
End With
End Sub

The above method will activate your custom OnKey commands for the duration you're working on the workbook the macro resides, that also means, as long as you're on subject workbook your "w, a, s, d" keys won't work for input.

There are more than one way to assign/activate the OnKey commands...
If you want to turn the OnKey commands "On" and "Off" while working in your workbook on demand, then copy the following codes in a standard module:

Code:
Sub ActivateOnKey()
With Application
    .OnKey ("w"), "moveup"
    .OnKey ("a"), "moveleft"
    .OnKey ("s"), "moveright"
    .OnKey ("d"), "movedown"
End With
End Sub

Sub DeActivateOnKey()
With Application
    .OnKey ("w")
    .OnKey ("a")
    .OnKey ("s")
    .OnKey ("d")
End With
End Sub

This method allows you to turn "On" or "Off" your custom OnKey commands by running the appropriate macro as named, that means, when "ActivateOnKey" is ran, your "w, a, s, d" keys work like the arrow keys, but once "DeActivateOnKey" is ran, your "w, a, s, d" keys can be used normally for input.

Let me know how it works out for you.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

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