Easy keymapping

Posted By: Inestical

Easy keymapping - 03/12/08 09:06

People nowdays need to be able to modify the key mapping, so using key_w, key_s, etc. will not be tolerated. That's why one should use strings or other method for checking which key was pressed.

No credits required, but note that this was also partially Kiyaku's idea.

For this I made this simple script:

Code:

// Key mapping
string keyForward = "w";
string keyBackward = "s";
string keyStrafeLeft = "a";
string keyStrafeRight = "d";
string keyRun = "shiftl";
string keyJump = "space";
string keyStrafe = "ctrll";

function getKey(key)
{
return (key_pressed(key_for_str(key)));
}



usage
Code:

if(getKey(keyForward))
{
//Do Stuff
}
// returns true/1/on wether button associated to forward movement is pressed



Key mapping can be found in the manual. Look for function "key_for_str" for complete list of accepted strings. AFAIK it's just key_blaa without the "key_".

Edit: This should work on mouse, but I'm not completely sure.
Posted By: Scorpion

Re: Easy keymapping - 03/13/08 20:37

hey, nice way to do that.
I just wonder what would be the best way to save the changed keymapping in a file...and to load it
Posted By: Xarthor

Re: Easy keymapping - 03/13/08 20:45

I always use an array and save the scan codes inside of it.
But your solution is also pretty nice, however I prefer the array because of saving/loading (just cycling through).
Posted By: Scorpion

Re: Easy keymapping - 03/13/08 20:49

yeah that is good Xarthor...
I think the best way would be an array of strings you can acces with defines(i.e. #define keyForeward 0)
and then there is also no problem with saving and loading

Thank you guys, you solved my problem
Posted By: Inestical

Re: Easy keymapping - 03/14/08 12:16

Using defines won't allow you to change the keymapping without restart (same as using key_lol), so using strings, maybe in an array, is way better. Also you can use the strings themselves for display, and not to rewrite them.

well, I use this to set the new key

Code:

function setKey()
{
return (str_for_key(key_lastpressed));
}



Usage:
Code:

...
if(forwardchange == 1)
{
keyForward = setKey();
}
...


Posted By: mpdeveloper_B

Re: Easy keymapping - 03/14/08 14:27

Xarthor this is pretty good, i assume this is from my post about how to do this. I didn't think of doing it this way, but the problem is that str_for_key doesn't support mouse functions it's keyboard exclusive. i will be starting on one that will support mouse and joystick all in one, but it will take some time because i have to use the key numbers and then tell it to convert it to the key name "ie 1 = esc".

thanks for the keyboard controls this would be very helpful for a puzzle game or any game that requires keyboard
Posted By: Xarthor

Re: Easy keymapping - 03/14/08 16:17

@Inestical:
You are right, you could use the text array for displaying the key mapping aswell.
But (now here comes the but ) in my humble oppinion the normal/standard user might be confused by the names.
Thus I did take my time to write some kind of hugh text array ranging to the highest scan code.
It includes a name for the keys (that I can change etc.) but maintains the right order.
Thus it allows me to access this text array with the scan code of the key and I'll be able to get the string I chosed for that specific key.

just an idea
Posted By: harrisyu

Re: Easy keymapping - 03/14/08 16:53

I think using number instead of string would be nicer.
Because string comparing take times.

Here is what I though about customize control in game:

A function for returning string with given input,like:
PlayerMoveForwardKey = getContStr( theControlNumberOfInput );
That's just for displaying what input have set for a certain action;

Then we store 'theControlNumberOfInput' in a value , use this value in
the function that handling control reaction.
Posted By: Scorpion

Re: Easy keymapping - 03/14/08 16:58

here i did a quick code how i mean it. Should work, but I didn't test it:

Code:
#define keyForward 0
#define keyBackward 1
#define keyStrafeLeft 2
#define keyStrafeRight 3
#define keyRun 4
#define keyJump 5
#define keyStrafe 6

#define numberOfKeys 7

STRING* keyMappingStr[numberOfKeys];

void mappingInit()
{
int i;
int fileHandle;
fileHandle = file_open_read("keyMapping");
for(i=0;i<numberOfKeys;i++)
{
keyMappingStr[i] = file_str_read(fileHandle);
}
file_close(fileHandle);
}

int getKey(key)
{
return (key_pressed(key_for_str(keyMappingStr[key])));
}


Posted By: Wicht

Re: Easy keymapping - 03/14/08 18:52

deleted by Wicht
Posted By: frazzle

Re: Easy keymapping - 03/15/08 08:55

Nice snippet Inestical, quite helpful

Cheers

Frazzle
© 2024 lite-C Forums