Key codes[solved.. sort of]

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
monkeycracks
Posts: 1029
Joined: Thu Apr 06, 2006 12:45 am
Location: Tennesee, USA
Contact:

Key codes[solved.. sort of]

Post by monkeycracks »

I know I can go here http://irrlicht.sourceforge.net/docu/na ... .html#a183 and see the key codes, but I can't see which number is associated with them (such as w is 87 or something).

Anyone know how to do this?
Last edited by monkeycracks on Sun Oct 07, 2007 9:26 am, edited 1 time in total.
Perceval
Posts: 158
Joined: Tue May 30, 2006 2:42 pm

Post by Perceval »

I can't see any interest in knowing which number is associated to a key code, but this is an enumerated type, so i guess the first one, KEY_LBUTTON, is zero, the second KEY_RBUTTON is one, etc, ...
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

right, the first (upper) key code is 0, the 2nd is 1, 3rd is 2, and so on... ;)
but you can easily get the code with (for example):

Code: Select all

printf("%d\n", KEY_KEY_A);
but I also don't see for what it's neccessary !?!?! :shock:
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
monkeycracks
Posts: 1029
Joined: Thu Apr 06, 2006 12:45 am
Location: Tennesee, USA
Contact:

Post by monkeycracks »

printf("%d\n", KEY_KEY_A);

That's how I've been finding them, and because I'm reading numbers from a file and casting them to a keymap

@Perceval, yes but when it gets further down the list in the docs.. that's more counting I have to do >_>
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

You shouldn't need to know the number that is associated with the enum. Everywhere you would use the number you should use the enum instead. That is why enums exist- to give a unique name to an integer value.

Maybe you could provide a simple testcase to illustrate why you would want to use or know the actual numeric value that one of the enums represents?

Travis
monkeycracks
Posts: 1029
Joined: Thu Apr 06, 2006 12:45 am
Location: Tennesee, USA
Contact:

Post by monkeycracks »

if (!strcmp(tag, "KMFo"))toload->keyMap[0].KeyCode = (EKEY_CODE)atoi(line);

if the prefix is KMFo, it'll load the integer after it and set the keycode to that integer (that stands for Keymap Move Forward)
I do this for strafe left, strafe right, move forward, and move backward

It works to just find the key doing printf(key), but I'm trying to print each key and its code to a file so I can use it for reference
FriendlyWarlord
Posts: 58
Joined: Thu Apr 06, 2006 11:54 pm

Post by FriendlyWarlord »

Can't you cast it to an int? My input handling stuff uses integers instead of Irrlicht key codes. Whenever I need to use a keycode for anything, I cast it to an int (or rather, I'm passing the keycode to a method as an int parameter). Because I always convert them to integers before using them, I never need to convert them back to Irrlicht key codes. And so, my crappy input handler becomes slightly less dependent on the graphics engine!
=D
monkeycracks
Posts: 1029
Joined: Thu Apr 06, 2006 12:45 am
Location: Tennesee, USA
Contact:

Post by monkeycracks »

I can't cast KEY_KEY_W to an int if pulling it out of a file because its a string. That would be saying (int)"KEY_KEY_W" =\

I have the integers containing the keycode values in the file, but I don't want to have to do printf("%d\n",KEYCODE) everytime I want to find one
FriendlyWarlord
Posts: 58
Joined: Thu Apr 06, 2006 11:54 pm

Post by FriendlyWarlord »

Ah, so you're just trying to write out a text file that contains a list of all the key codes, and the number for each? There probably isn't a way to turn the name of the key (for example "KEY_KEY_W") into a string.

But if I wanted a list of all the key codes with their numerical values, I'd look in Irrlicht's source where the key codes are declared. Copy and paste the list into a text file. If you've got time on your hands, you can type in the number for each yourself... but to automate it, just read that text file, and spit out another one with line numbers.
Again, I'm curious why you'd need to know the numerical values associated with the keys, please tell!


If you're trying to print out the names of the keys in your game, here's the solution I'm using:

Every key (and mouse button) is an instance of the InputKey class, which contains both the Irrlicht key code, and the name of the key. At the start of the program, all the keys need to be initialized:

createKey( irr::KEY_LBUTTON, "Left Click" );
createKey( irr::KEY_RBUTTON, "Right Click" );
createKey( irr::KEY_MBUTTON, "Middle Click" );
createKey( irr::KEY_RETURN, "Enter" );
createKey( irr::KEY_TAB, "Tab" );
etc.

Now when I want to make a GUI for customizing the keys, all the keys have nice names! I'm a big fan of OO, I'm trying to hide Irrlicht's input handling stuff as much as possible (without losing any speed).
=D
monkeycracks
Posts: 1029
Joined: Thu Apr 06, 2006 12:45 am
Location: Tennesee, USA
Contact:

Post by monkeycracks »

Because the numerical values associated with the keys are in the options file, which are then read and assigned to the SKeyMap. I suppose I could take the time to construct the file by hand, but it'd be easier to automate it.
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

Umm the first key starts with 1! Not 0!

If you need proof. Here are all the keycodes http://irrlicht.sourceforge.net/docu/_k ... ource.html
TheQuestion = 2B || !2B
monkeycracks
Posts: 1029
Joined: Thu Apr 06, 2006 12:45 am
Location: Tennesee, USA
Contact:

Post by monkeycracks »

I failed to see how that was relevant.
Perceval
Posts: 158
Joined: Tue May 30, 2006 2:42 pm

Post by Perceval »

Maybe it's starting from zero... :lol:
Anyway, i really think that when you use a lib like Irrlicht, you have to make abstraction of its implementation. If tomorrow, the Irrlicht's team decide to start key codes from 1256, you'll have to change your code too. That's why you should use key_code, without trying to know which integer is associated to them.
In addition, i think the file that contains your keymap is not supposed to be manually editing by the user, so you can simply cast the key code into an integer, what would be a lot easier :wink:
monkeycracks
Posts: 1029
Joined: Thu Apr 06, 2006 12:45 am
Location: Tennesee, USA
Contact:

Post by monkeycracks »

I think I made things really foggy..

What I do to save the file is get the Key_Code and cast it to an integer and save it to the file. I was just wondering about the keycodes because I manually constructed the file the first time I did it.

Why? I don't know.
Its all fixed now for the time being
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

Perceval wrote:Maybe it's starting from zero... :lol:
Anyway, i really think that when you use a lib like Irrlicht, you have to make abstraction of its implementation. If tomorrow, the Irrlicht's team decide to start key codes from 1256, you'll have to change your code too. That's why you should use key_code, without trying to know which integer is associated to them.
In addition, i think the file that contains your keymap is not supposed to be manually editing by the user, so you can simply cast the key code into an integer, what would be a lot easier :wink:
They would never happen. For the simple fact the class contains the value of the keys mapped on the keyboard. Which means if you change the value, then you change the key. There really is no need to use the abstraction enum for keys beside the fact that it makes code more readable.
TheQuestion = 2B || !2B
Post Reply