1
# Xlib.XK -- X keysym defs
3
# Copyright (C) 2000 Peter Liljenberg <petli@ctrl-c.liu.se>
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
# This module defines some functions for working with X keysyms as well
20
# as a modular keysym definition and loading mechanism. See the keysym
21
# definition modules in the Xlib/keysymdef directory.
23
from X import NoSymbol
25
def string_to_keysym(keysym):
26
'''Return the (16 bit) numeric code of keysym.
28
Given the name of a keysym as a string, return its numeric code.
29
Don't include the 'XK_' prefix, just use the base, i.e. 'Delete'
30
instead of 'XK_Delete'.'''
31
return globals().get('XK_' + keysym, NoSymbol)
33
def load_keysym_group(group):
34
'''Load all the keysyms in group.
36
Given a group name such as 'latin1' or 'katakana' load the keysyms
37
defined in module 'Xlib.keysymdef.group-name' into this XK module.'''
39
raise ValueError('invalid keysym group name: %s' % group)
41
G = globals() #Get a reference to XK.__dict__ a.k.a. globals
43
#Import just the keysyms module.
44
mod = __import__('Xlib.keysymdef.%s' % group, G, locals(), [group])
46
#Extract names of just the keysyms.
47
keysyms = [n for n in dir(mod) if n.startswith('XK_')]
49
#Copy the named keysyms into XK.__dict__
50
for keysym in keysyms:
51
## k = mod.__dict__[keysym]; assert k == int(k) #probably too much.
52
G[keysym] = mod.__dict__[keysym]
54
#And get rid of the keysym module.
57
def _load_keysyms_into_XK(mod):
58
'''keysym definition modules need no longer call Xlib.XK._load_keysyms_into_XK().
59
You should remove any calls to that function from your keysym modules.'''
62
# Always import miscellany and latin1 keysyms
63
load_keysym_group('miscellany')
64
load_keysym_group('latin1')
67
def keysym_to_string(keysym):
68
'''Translate a keysym (16 bit number) into a python string.
70
This will pass 0 to 0xff as well as XK_BackSpace, XK_Tab, XK_Clear,
71
XK_Return, XK_Pause, XK_Scroll_Lock, XK_Escape, XK_Delete. For other
72
values it returns None.'''
74
# ISO latin 1, LSB is the code
75
if keysym & 0xff00 == 0:
76
return chr(keysym & 0xff)
78
if keysym in [XK_BackSpace, XK_Tab, XK_Clear, XK_Return,
79
XK_Pause, XK_Scroll_Lock, XK_Escape, XK_Delete]:
80
return chr(keysym & 0xff)
82
# We should be able to do these things quite automatically
83
# for latin2, latin3, etc, in Python 2.0 using the Unicode,
84
# but that will have to wait.