This is a topic that I have been grappling with recently.
Another thing to check here is if you are getting correct values in different keyboard layouts. For example it doesn't look like pygame reports number key codes correctly when you switch into a French - PC layout. My suggestion is to use wxPython. It exposes the platform specific key codes in the RawKeyCode property.
Here is a code sample which demonstrates that:
import logging as log
import wx
class MainWindow(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(200,100))
self.panel = wx.Panel(self, wx.ID_ANY)
self.panel.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
self.panel.Bind(wx.EVT_KEY_UP, self.OnKeyDown)
self.panel.Bind(wx.EVT_CHAR, self.OnKeyDown)
self.panel.SetFocus()
self.Show(True)
def OnKeyDown(self, event=None):
line_txt = '---------------------'
print(line_txt)
for var in ['RawKeyCode', 'KeyCode', 'UnicodeKey']:
print(var, getattr(event, var))
if var == 'UnicodeKey':
print('char', chr(getattr(event, var)))
print(line_txt)
if __name__ == "__main__":
app = wx.App(False)
gui = MainWindow(None, "test")
app.MainLoop()
For example when I hit the fn key on my mac (a mac-only key) I see this logged:
---------------------
RawKeyCode 63
KeyCode 0
UnicodeKey 0
char
---------------------
Which is consistent with the platform specific key codes here.