Very basic key mapping demo - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1) +--- Forum: Prolific Programmers (https://qb64phoenix.com/forum/forumdisplay.php?fid=26) +---- Forum: SMcNeill (https://qb64phoenix.com/forum/forumdisplay.php?fid=29) +---- Thread: Very basic key mapping demo (/showthread.php?tid=3382) |
Very basic key mapping demo - SMcNeill - 01-15-2025 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: Code: (Select All)
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. |