Kivy (pygame) KeyCodes to ASCII Keycodes?

952 views
Skip to first unread message

Syn

unread,
Nov 14, 2012, 5:48:38 PM11/14/12
to kivy-...@googlegroups.com
In Kivy when catching keypress keycodes we will receive different key codes rather than the ASCII keycodes.

For Example, In Kivy:

273 UP Arrow 
274 DOWN arrow 
275 RIGHT arrow 
276  LEFT  arrow


The Ascii Keycodes,

Left Arrow
37 

Up Arrow
38 

Right Arrow
39 

Down Arrow
40


Is there a way to convert Kivy keycodes to ASCII keycodes? The best that I can think of is, making a large dictionary comparing both however this would be not the ideal solution.

T500

unread,
Nov 14, 2012, 10:05:45 PM11/14/12
to kivy-...@googlegroups.com
Excuse me for being a few brain cells short, but when I last checked ASCII stands for American Standard Code for Information Interchange...
And your looking to return the following example values for the cursor keys...

Left Arrow = ASCII 37 = % 
Up Arrow = ASCII 38 = &
Right Arrow = ASCII 39 = '
Down Arrow = ASCII 40 = (

...just checking ?

Kivy on_keyboard_down( keyboard, keycode, text, modifiers) 


For example for left arrow returns in keycode = (276, 'left')
Thus keycode[0]=276 is the keycode and keycode[1]  = ascii char string of the key pressed, which in the case of the cursor keys contains the string 'left'. For the cursor keys text=''.

Hope that helps?

Syn

unread,
Nov 14, 2012, 11:00:07 PM11/14/12
to kivy-...@googlegroups.com
I would like to get  37 as the keycode  when the user presses the Left Arrow key however it is giving me 276.

skeezix

unread,
Nov 14, 2012, 11:18:25 PM11/14/12
to kivy-...@googlegroups.com
On Wed, 14 Nov 2012, Syn wrote:

# I would like to get  37 as the keycode  when the user presses the Left Arrow key however it is giving me 276.

Those are not ASCII - the cursor keys are not represented in ASCII
at all. Recall, ASCII is (mostly) for _display_ and you do not display
cursor keys :) There is 7bit ascii and 'extended ascii' for some extra
characters and so forth... but you receive scancodes that are device and
OS specific and then map them to some storage medium (which could be
ASCII, unicode, EBCDIC, etc..)

.. you could map them keycode to whatever you want, though.. so
why not map it to 37 if you want?

jeff

--
If everyone would put barbecue sauce on their food, there would be no war.

Syn

unread,
Nov 15, 2012, 7:20:15 AM11/15/12
to kivy-...@googlegroups.com
I would have to do that manually for every key right? I was hoping for a built-in type of way so I wouldn't have to do that.

Gabriel Pettier

unread,
Nov 15, 2012, 7:43:21 AM11/15/12
to kivy-...@googlegroups.com
The code you are asking for is not ascii, as said above, because ascii simply doesn't contain arrows (and as pointed by T500, the codes you are asking for means different things than arrows in ascii) , it only contains printable caracters. So if it's not made up (or buggy code that happens to work on some setup), there may be something to convert, but without knowing what it is, we can't tell if there is such a thing. You can see here http://kivy.org/docs/api-kivy.core.window.html that the code used comes from pygame (http://www.pygame.org/docs/ref/key.html) and that you can use the text of the key, rather than the code, if you directly want "up/down/left/right" to test for.
--
 
 

Reply all
Reply to author
Forward
0 new messages