3 hours ago
A couple of people asked me about how to map keys to perform certain actions, such as for user input with a game. Some folks like "WASD" keys for movement. Others like ARROW keys. How complicated is it to let the user choose such keys for themselves??
At its heart, the process is simple as heck. Try out this little demo below:
Nothing fancy at all here, but this is basically the core of what you're looking for, to add this type of functionality to your own programs.
Clean it up, make it graphical, select only one button to change at a time... There's a bazillion ways you can expand this to make it fancier or prettier, but the base idea is the same:
Have a prompt of some sort that the end user can interact with and use it to capture the keypress that they want to associate with that prompt.
Note that there's no error-checking here, so you could use SPACE for all 6 keyboard inputs in this demo. (Don't write serious programs like this, add in that error checking to avoid duplicates!)
Note two: Also notice that I've invalidated all modifier keys and modified keypresses for my key mapping. SHIFT-ESC will end the program, and there's no way for the user to map their custom keystrokes into my SHIFT-keystrokes to cause conflicts. Be careful of this, if you want to reserve certain functionality in your own programs for system usage like this.
Note three: Most games have at least six buttons associated with them -- left, right, up, down for movement, and an OK and Cancel button for dialog or actions. I have no idea what my OK or Cancel buttons should do for a simple demo with a box moving across the screen, so just to do *something*, they'll allow you to change the size of the box. WHOO!! Please make your games fancier and more interactive than this.
But honestly, a lot of folks get lost trying to sort out how to do something like this as they tend to overthink it, when the truth is that it can really be THIS simple.
Just prompt for an input for an action. Save that input. <<-- That's all there is to it at the end of the day. Everything else is just bells and whistles to make it look fancier or seem more professional. The core process really is that easy.
At its heart, the process is simple as heck. Try out this little demo below:
Code: (Select All)
_Title "Simple Key Mapping Demo (Shift-ESC to quit)"
Screen _NewImage(800, 600, 32)
Dim Shared As Long Up, Down, Left, Right, OK, Cancel '6 buttons for this little demo
ResetKeys 'start off asking for the user's key preferences
'and a cheezy demo to use those keys
Size = 100
Do
Cls , 0
Line (x, y)-Step(Size, Size), &HFFFF0000, BF 'a box to move with our keys!
k = _KeyHit
xmod = 0: ymod = 0: sizemod = 0
If _KeyDown(Up) Then ymod = -5 Else If _KeyDown(Down) Then ymod = 5
If _KeyDown(Left) Then xmod = -5 Else If _KeyDown(Right) Then xmod = 5
If _KeyDown(OK) Then sizemod = -5 Else If _KeyDown(Cancel) Then sizemod = 5
If (_KeyDown(100304) _Orelse _KeyDown(100303)) _Andalso _KeyDown(27) Then System
Size = _Clamp(Size + sizemod, 50, 250)
x = _Clamp(x + xmod, 0, 800 - Size)
y = _Clamp(y + ymod, 0, 600 - Size)
_Limit 30
_Display
Loop
Sub ResetKeys
Cls
Print "Please press the key for UP :";: Up = GetKey: Print Up
Print "Please press the key for DOWN :";: Down = GetKey: Print Down
Print "Please press the key for LEFT :";: Left = GetKey: Print Left
Print "Please press the key for RIGHT:";: Right = GetKey: Print Right
Print "Please press the key for OK:";: OK = GetKey: Print OK
Print "Please press the key for CANCEL:";: Cancel = GetKey: Print Cancel
End Sub
Function GetKey
_KeyClear
Do
_Limit 30
k = _KeyHit
Loop Until k > 0 And k < 100000 'ignore key up and modifier style keys
GetKey = k
End Function
Nothing fancy at all here, but this is basically the core of what you're looking for, to add this type of functionality to your own programs.
Clean it up, make it graphical, select only one button to change at a time... There's a bazillion ways you can expand this to make it fancier or prettier, but the base idea is the same:
Have a prompt of some sort that the end user can interact with and use it to capture the keypress that they want to associate with that prompt.
Note that there's no error-checking here, so you could use SPACE for all 6 keyboard inputs in this demo. (Don't write serious programs like this, add in that error checking to avoid duplicates!)
Note two: Also notice that I've invalidated all modifier keys and modified keypresses for my key mapping. SHIFT-ESC will end the program, and there's no way for the user to map their custom keystrokes into my SHIFT-keystrokes to cause conflicts. Be careful of this, if you want to reserve certain functionality in your own programs for system usage like this.
Note three: Most games have at least six buttons associated with them -- left, right, up, down for movement, and an OK and Cancel button for dialog or actions. I have no idea what my OK or Cancel buttons should do for a simple demo with a box moving across the screen, so just to do *something*, they'll allow you to change the size of the box. WHOO!! Please make your games fancier and more interactive than this.
But honestly, a lot of folks get lost trying to sort out how to do something like this as they tend to overthink it, when the truth is that it can really be THIS simple.
Just prompt for an input for an action. Save that input. <<-- That's all there is to it at the end of the day. Everything else is just bells and whistles to make it look fancier or seem more professional. The core process really is that easy.