List of Python key codes?

Hey guys,

I need a list of numeric key codes for BGE Python. The list on tutorialsforblender3d.com seems to have been taken down.

And please, give it to me straight. I don’t want “alternate ways of doing it”. I want a list of the numeric key codes.

Numeric? That’s a bit odd… Here is the list of numeric equivalent constants. If you really need the numeric codes you can print the constant, and it should print the numeric equivalent. Other than tutorialsforblender3d I don’t know of another place that has a list of the numeric codes.

Furthering from kendrick’s post, you shouldn’t use constants by value. They’re not meaningful, and serve to require someone to look up what the value represents, which can be difficult if it is not documented meaningfully (I.e which constants can be meaningfully compared for a certain value)
With that in mind you can print the constants that are available from the logic module :


from inspect import getmembers
from bge import logic

constants = {k:v for k,v in getmembers(logic) if k.starts with('KX_')}

See, this is what I’m trying to do.
http://bgepython.tutorialsforblender3d.com/KeyboardSensor/getKeyStatus
I need the numeric codes for getKeyStatus.
Unless I don’t, and I’m just being a stupid person about it. Which is most probable.

You can think of the constants as being a type of variable (one that doesn’t change) it basically assigns each of those attributes a certain value. So events.AKEY is equivalent to 97, events.WKEY is equivalent to 119, etc…

It is the same as those status constants. Take KX_INPUT_JUST_ACTIVATED for instance. This is really just a more read-able way of writing a 1 in your code (I think it was 1). Both ways will work, but it is easier as a programmer to know what the intent of KX_INPUT_JUST_ACTIVATED is.

So yes you can (and should) use the constants in place of the “raw” key codes. So if you want the status of the “A” key:

from bge import logic, events

controller = logic.getCurrentController()
keyboardSensor = controller.sensors['Keyboard']
aKeyStatus = keyboardSensor.getKeyStatus(events.AKEY)

This is really a more readable version of:

from bge import logic

controller = logic.getCurrentController()
keyboardSensor = controller.sensors['Keyboard']
aKeyStatus = keyboardSensor.getKeyStatus(97)

hi MichelleSea,

The list of keycodes is still on tutorialsforblender3d.com. I redid the menus and forgot to update the link. I fixed the link on that page. It now works.

Here is the correct link: Game Keys Menu

Clark

Oh my goodness, thank you so much.

And while I’m here, I’d like to say that I love and appreciate your site. It’s solved a lot of my python problems. Thank you for maintaining it so well over the years. =D

The official API is still there :wink:

I find this interesting. Because for me it’s always easier to type the keycode and 1,2,3 for status instead of using:
bge.events.WKEY
or
bge.logic.KX_INPUT_JUST_ACTIVATED

It is more important that it is easy to read and understand, rather than to write.

Maybe if you’re planning on opening the source of your game or getting more than one programmer, which are things I’m not doing.
All that matters in this case is that I can read it easily, and I can read those numbers just fine.
Besides, it’s not hard to memorize specific key codes, especially for letters once you remember that D=100.

No, readable code is not for others. You come back in a week, you forget the purpose of some obscure constants, it’s much more important to write verbose code. We spend far longer reading than writing, by nature.

In a more complex system, I would agree. But we are talking about simple keycodes here. If a script is about keyboard events it’s easy and logical (actually more logical for me) to read those numbers, instead of verboses. But that’s just me. I don’t want to cause an argue really, just wished to show the other side of the coin here :slight_smile:

Well, google answers another question. I’m going to setup a module that the user can set the keys for movement. It is going to be a collections of modules to automate writing scripts, reducing lines of code, and maybe completely removing the need to write scripts for some games. Thanks for this thread, I’m going to write a module with each key assigned to it’s numeric equivalent so the player can use specific keys in a qwerty keyboard. Thanks guys.