2 registered members (AndrewAMD, juanex),
1,247
guests, and 6
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Keybinds
#460996
07/22/16 13:13
07/22/16 13:13
|
Joined: Jun 2009
Posts: 2,210 Bavaria, Germany
Kartoffel
OP
Expert
|
OP
Expert
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
|
Does anyone know a good approach for implementing keybinds?
I already have an idea but there's probably a much better solution.
POTATO-MAN saves the day! - Random
|
|
|
Re: Keybinds
[Re: Kartoffel]
#460997
07/22/16 13:25
07/22/16 13:25
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
Lookup table if you want to poll, lookup table with function pointers if you want to have callbacks.
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: Keybinds
[Re: Kartoffel]
#460998
07/22/16 13:28
07/22/16 13:28
|
Malice
Unregistered
|
Malice
Unregistered
|
^-- Or that --- Yes definitely THAT!
Str_for_key to ->>TEXT key_for_str return ->> scancode to array[max_key-in_order]
Key_hit / key_pressed / key_lastpressed /key_any combinations for the rest.
Last edited by Malice; 07/22/16 13:29.
|
|
|
Re: Keybinds
[Re: WretchedSid]
#460999
07/22/16 13:37
07/22/16 13:37
|
Joined: Jun 2009
Posts: 2,210 Bavaria, Germany
Kartoffel
OP
Expert
|
OP
Expert
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
|
@JustSid: So you mean I create an array of bytes/ints (whichever I need) where each element represents one key of the keyboard (keycode = index) to store an id to what that keyshould do? Sounds good
I thought about it the other way around. Storing the corresponding keycode for each action (jump, walk left, walk right, etc.) and scanning that key everytime the action is needed (or scanning the keys for all actions at the start of each frame and storing the results using a struct).
Edit: I just have to think about how to do this in acknex... afaik there's no function that takes a keycode and returs whether or not this key is pressed.
Edit2: I'll probably go for the windows api and do that stuff myself. Not sure about gamepad input, though.
Last edited by Kartoffel; 07/22/16 13:48.
POTATO-MAN saves the day! - Random
|
|
|
Re: Keybinds
[Re: Kartoffel]
#461000
07/22/16 13:47
07/22/16 13:47
|
Malice
Unregistered
|
Malice
Unregistered
|
Key_hit from keys.c
opps maybe keycode and scancode are different.. Sorry, I'm in over my head and up too many hours awake.
Last edited by Malice; 07/22/16 13:49.
|
|
|
Re: Keybinds
[Re: ]
#461001
07/22/16 13:53
07/22/16 13:53
|
Joined: Jun 2009
Posts: 2,210 Bavaria, Germany
Kartoffel
OP
Expert
|
OP
Expert
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
|
@Malice: cool thanks! didn't know about that one (let's hope it actually includes at least most keyboard buttons or otherwise.. winapi)
Edit: google just told me most keyboards have ~104 keys and since acknex is using an array with 256 indices for keypresses it really includes all of them I guess.
keycode, scancode... Well, I meant the same thing as you. But no idea if there's an actual difference between those two lol
Last edited by Kartoffel; 07/22/16 13:57.
POTATO-MAN saves the day! - Random
|
|
|
Re: Keybinds
[Re: Kartoffel]
#461006
07/22/16 15:15
07/22/16 15:15
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
Actually I meant that you have a table with an entry for every action that a key can be bound to and then the keycode for the key that is bound to that action. Then you can just poll the corresponding entry if/when needed. Key binding itself is then as simple as: Wait for any input Update the slot in the lookup table And tada, your keybinding has O(1) lookup time. Something like this (pseudo-pseudo code):
enum Action
{
WalkForward,
Jump,
__Max
}
KeyCodeType LookupTable[Action::__Max] = { 0 };
void BindKeyToAction(Action action)
{
KeyCodeType input = GetNextKeyPress();
LookupTable[action] = input;
}
void GameLoop()
{
while(1)
{
if(HasInput(LookupTable[Action::Jump])
Jump();
}
}
Last edited by WretchedSid; 07/22/16 15:19.
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: Keybinds
[Re: WretchedSid]
#461009
07/22/16 16:59
07/22/16 16:59
|
Joined: Jun 2009
Posts: 2,210 Bavaria, Germany
Kartoffel
OP
Expert
|
OP
Expert
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
|
@JustSid: oh okay, so it's pretty close to what I had in mind at first. However, I think I'll do the polling once every frame for all actions, store the data in a struct and pass it to the game Logic function(s).
Thank you two again for your help.
POTATO-MAN saves the day! - Random
|
|
|
Re: Keybinds
[Re: txesmi]
#461011
07/22/16 17:31
07/22/16 17:31
|
Joined: Dec 2011
Posts: 1,823 Netherlands
Reconnoiter
Serious User
|
Serious User
Joined: Dec 2011
Posts: 1,823
Netherlands
|
Are not events on_a, on_b, etc key binds already? Do I missundestood something? , I think he means customizable keybindings for e.g. player controls (like e.g. default would be WASD for movement but than an user could change it to the arrow keys). I personally use this method now (super easy to setup and seems to work great): http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=188074However it does not support the mouse(!). But some extra sliders and buttons for some presets could sort of fix that (I think ).
|
|
|
|