dMZX Forums: Custom Keybinding Engine - dMZX Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Custom Keybinding Engine

#1 User is offline   Old-Sckool 

  • megazeux breaker
  • PipPipPipPip
  • Group: Members
  • Posts: 649
  • Joined: 07-June 05
  • Gender:Male

Posted 03 December 2012 - 05:23 AM

Before I start, I should note that I'm probably not explaining what this engine does very well.

This is a pretty simple key binding engine that allows you to create your own custom key counters. It's useful if you want to let the player reassign keys, or to remove complex input logic like detecting if the player has just pressed a key (ignoring holding).


There are three parts to the engine.

The first component is the keybinding list (K). This list binds a set of counters to a key_code and optionally a key_pressed value. You can initialize one keybind with the following code.

set "$k&k&" to "kRight"
set "k&k&_code" to 17
set "k&k&_press" to 119
inc "k" by 1


k&k&_code stores the key_code value used for key detection
k&k&_press is optional and stores the key_pressed value used for key detection with autorepeat. Not sure if this works right since I usually use key_code.
k is simply the length of the list
$k&k& stores a pointer to the set of counters that the engine will update every cycle. For this example, setting "$k&k&" to kRight will cause the engine to assign the following counters every cycle

kRight will be set to 1 if W is held, or 0 if W is released, ignoring autorepeat. Uses the key_code value stored in k&k&_code
kRight_1 will be set to 1 ONLY if the W key has just been pressed. Uses the key_code value stored in k&k&_code
kRight_w is reserved by the engine.
kRight_p will be set to 1 when the W key is held, but is 0 when autorepeating. Uses the key_pressed value stored in k&k&_press


The second part is a simple loop that makes it easier to find the elements in the keybinding list if you want to reassign keys

. "loop once"
loop start
set "&$k('loopcount')&_k" to "loopcount"
loop for "('k'-1)"


With our kRight example above, it will store the index to the kRight bindings in kRight_k, if you want to reassign what key is bound to kRight. For example, to bind kRight to the D key instead of the W key, you would simply change the k&k&_code and k&k&_press values.

set "k&kRight_k&_code" 32
set "k&kRight_k&_press" 100



The last part is the loop which assigns the variables every cycle. This loop should be executed every cycle by the global robot, and should be one of the first things executed.

: "#bindings"
loop start
set "&$k('loopcount')&" to "key('k&loopcount&_code')"
set "&$k('loopcount')&_1" to "('key('k&loopcount&_code')'a('k&loopcount&_w'=0))"
set "k('loopcount')_w" to "key('k&loopcount&_code')"
if "k&loopcount&_press" = 0 then "skip"

set "&$k('loopcount')&_p" to "('k&loopcount&_press'='key_pressed')"

: "skip"
loop for "('k'-1)"
goto "#return"




Here's a slightly modified version I used in my terrible doz entry with everything together

set "commands" to "(90000)"

set "$k&k&" to "kShootN"
set "k&k&_code" to 17
inc "k" by 1

set "$k&k&" to "kShootS"
set "k&k&_code" to 31
inc "k" by 1

set "$k&k&" to "kShootE"
set "k&k&_code" to 32
inc "k" by 1

set "$k&k&" to "kShootW"
set "k&k&_code" to 30
inc "k" by 1

set "$k&k&" to "kSkip"
set "k&k&_code" to 31
inc "k" by 1

set "$k&k&" to "kHint"
set "k&k&_code" to 35
inc "k" by 1

set "$k&k&" to "kUp"
set "k&k&_code" to 72
inc "k" by 1

set "$k&k&" to "kDown"
set "k&k&_code" to 80
inc "k" by 1

set "$k&k&" to "kLeft"
set "k&k&_code" to 75
inc "k" by 1

set "$k&k&" to "kRight"
set "k&k&_code" to 77
set "k&k&_press" to 275
inc "k" by 1

. "loop once"
loop start
set "&$k('loopcount')&_k" to "loopcount"
loop for "('k'-1)"

: "q"
cycle 1
goto "#bindings"
* "~f&kRight_1& &kRight& &kRight_p&"
goto "q"

: "#bindings"
loop start
set "&$k('loopcount')&" to "key('k&loopcount&_code')"
set "&$k('loopcount')&_1" to "('key('k&loopcount&_code')'a('k&loopcount&_w'=0))"
set "k('loopcount')_w" to "key('k&loopcount&_code')"
if "k&loopcount&_press" = 0 then "skip"

set "&$k('loopcount')&_p" to "('k&loopcount&_press'='key_pressed')"

: "skip"
loop for "('k'-1)"
goto "#return"

<Nadir> mzxers don't make GAMES, usually
<phthalocyanine> they make experiences.
<Nadir> demos, more like
<Nadir> a glimpse into what could have been if mzx wasn't such a bore to work with
<Nadir> actually, i'm being unfair
<Nadir> i would have made mzx games if it was capable of running on more than 20 computers worldwide in 1998
<Nadir> >:D

<%Alice> functor
<%nooodl> i hear C++ has a thing called functors and they're completely different from Haskell functors...
<rorirover> the result is the most horrid thing in C++, it's basically black magic and it transforms any code you're writing into some eldritch monstrosity
1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users