How to trigger a macro on arrow keys?

clut

Board Regular
Joined
Oct 5, 2006
Messages
74
I'm creating a small Excel RPG where the player moves the around a blank worksheet with the walls of a maze appearing as you approach them (think dark corridors, you can see only 1 cell in all directions around you).

The players current location is shown by excel setting the activecell to the players location. Currently I have four buttons labeled Up, Down, Left and Right which the player clicks to move in the desired direction.

The testers were frequently missing the four movement buttons and this would put the activecell outside of the maze next to one of the buttons. They'd have to move it back manually.

I would like for the players to move around using the arrow keys to use the activecell, as you would normally move around a sheet... but before each movement there is a procedure that check IF the player can move in the desired direction or not (cell borders used for walls), and then once they've moved there are more procedures to check for monsters treasure chests etc.

How can I make the arrow keys trigger the macros that the Up, Down, Left and Right buttons currently trigger, and not just move the activecell?
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
You could try a Worksheet_SelectionChange event. When a different cell(s) is selected, the macro is triggered.

Edit: You could also look into some OnKey events, but I'm not that great at those, myself. If you search the board, some threads should come up.
 
Upvote 0
Kirsty,

Where would I write the code for the event(s) you listed above? I generally write all my code first and then assign them to buttons via right-clicking, however I can't do that for the Worksheet_SelectionChange event, or onKey events.

I figured it'd be an onKey event of some flavour, as I've done similar things in Access, although in Access you've got the properties window that lists all the available events and allows you to assign procedures from there. How do you do this in Excel?
 
Upvote 0
Selection_Change would go in the WorkSheet module. Right click the sheet tab and choose "View Code"

lenze
 
Upvote 0
OnKey might be a good bet. The only problem with the Selection_Change event is that you will be responding to an action that has already taken place. This may or may not matter in your case. Throw all of the following into ThisWorkbook class and manually run Private Sub Workbook_Activate() or save, close, and reopen the workbook. Or download and open the example and then start clicking your arrow keys to see the results.

clut_042407.zip

<table width="100%" border="1" bgcolor="White" style="filter:progid:DXImageTransform.Microsoft.Gradient(endColorstr='#C0CFE2', startColorstr='#FFFFFF', gradientType='0');"><tr><TD><font size="2" face=Courier New>  <font color="#0000A0">Option</font> <font color="#0000A0">Explicit</font>

  <font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> Workbook_Activate()
       Application.OnKey "{LEFT}", "ThisWorkbook.OnLeftArrowKeyPress"
       Application.OnKey "{RIGHT}", "ThisWorkbook.OnRightArrowKeyPress"
       Application.OnKey "{UP}", "ThisWorkbook.OnUpArrowKeyPress"
       Application.OnKey "{DOWN}", "ThisWorkbook.OnDownArrowKeyPress"
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>

  <font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> Workbook_Deactivate()
       Application.OnKey "{LEFT}", ""
       Application.OnKey "{RIGHT}", ""
       Application.OnKey "{UP}", ""
       Application.OnKey "{DOWN}", ""
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>

  <font color="#0000A0">Friend</font> <font color="#0000A0">Sub</font> OnLeftArrowKeyPress()
       MsgBox "You pressed the Left arrow key..."
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>

  <font color="#0000A0">Friend</font> <font color="#0000A0">Sub</font> OnRightArrowKeyPress()
       MsgBox "You pressed the Right arrow key..."
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>

  <font color="#0000A0">Friend</font> <font color="#0000A0">Sub</font> OnUpArrowKeyPress()
       MsgBox "You pressed the Up arrow key..."
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>

  <font color="#0000A0">Friend</font> <font color="#0000A0">Sub</font> OnDownArrowKeyPress()
       MsgBox "You pressed the Down arrow key..."
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
</FONT></td></tr></table><button onclick='document.all("4242007182829125").value=document.all("4242007182829125").value.replace(/<br \/>\s\s/g,"");document.all("4242007182829125").value=document.all("4242007182829125").value.replace(/<br \/>/g,"");window.clipboardData.setData("Text",document.all("4242007182829125").value);'>Copy to Clipboard</BUTTON><textarea style="position:absolute;visibility:hidden" name="4242007182829125" wrap="virtual">
Option Explicit

Private Sub Workbook_Activate()
Application.OnKey "{LEFT}", "ThisWorkbook.OnLeftArrowKeyPress"
Application.OnKey "{RIGHT}", "ThisWorkbook.OnRightArrowKeyPress"
Application.OnKey "{UP}", "ThisWorkbook.OnUpArrowKeyPress"
Application.OnKey "{DOWN}", "ThisWorkbook.OnDownArrowKeyPress"
End Sub

Private Sub Workbook_Deactivate()
Application.OnKey "{LEFT}", ""
Application.OnKey "{RIGHT}", ""
Application.OnKey "{UP}", ""
Application.OnKey "{DOWN}", ""
End Sub

Friend Sub OnLeftArrowKeyPress()
MsgBox "You pressed the Left arrow key..."
End Sub

Friend Sub OnRightArrowKeyPress()
MsgBox "You pressed the Right arrow key..."
End Sub

Friend Sub OnUpArrowKeyPress()
MsgBox "You pressed the Up arrow key..."
End Sub

Friend Sub OnDownArrowKeyPress()
MsgBox "You pressed the Down arrow key..."
End Sub</textarea>

clut_042407.zip
 
Upvote 0
Thank you very much for your replies. The onKey example looks to be the perfect solution. I'll implement it later and let you know if it does the trick.
 
Upvote 0
Well the arrow keys work perfectly. They really add to the game (it was quite fiddly before having up, down, left and right buttons to click). Thank you again.
 
Upvote 0
I'd like to see this RPG when you're done with it.

MrByte,

Much of the work on the game is done. I wanted to keep it small, simple, and most importantly playable... which I feel it is.

I do need a playtester or two to help iron out balancing issues and things in the game. If you'd like to help with this (and by extension get an early peek at the game) let me know and I'll pm you a link when it's ready.

Currently most of the core coding is done. It just needs error checking, and handling, and user-proofing (hiding sheets not currently in use etc).
 
Upvote 0

Forum statistics

Threads
1,214,866
Messages
6,121,996
Members
449,060
Latest member
mtsheetz

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