mirror of
https://github.com/wassname/pyreadline.git
synced 2026-07-09 00:20:08 +08:00
Reformatted pyreadline/keysyms/*.py
This commit is contained in:
@@ -16,20 +16,20 @@ This was modeled after the C extension of the same name by Fredrik Lundh.
|
||||
import sys,os
|
||||
import traceback
|
||||
import re
|
||||
from pyreadline.logger import log
|
||||
from pyreadline.unicode_helper import ensure_unicode,ensure_str
|
||||
|
||||
import pyreadline.unicode_helper as unicode_helper
|
||||
|
||||
from pyreadline.logger import log
|
||||
from pyreadline.unicode_helper import ensure_unicode, ensure_str
|
||||
from pyreadline.keysyms import make_KeyPress
|
||||
from pyreadline.console.ansi import AnsiState,AnsiWriter
|
||||
|
||||
try:
|
||||
from ctypes import *
|
||||
from _ctypes import call_function
|
||||
except ImportError:
|
||||
raise ImportError(u"You need ctypes to run this code")
|
||||
|
||||
# my code
|
||||
from pyreadline.keysyms import make_KeyPress
|
||||
from pyreadline.console.ansi import AnsiState,AnsiWriter
|
||||
|
||||
|
||||
def nolog(string):
|
||||
pass
|
||||
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
import sys
|
||||
|
||||
success=False
|
||||
in_ironpython=u"IronPython" in sys.version
|
||||
success = False
|
||||
in_ironpython = u"IronPython" in sys.version
|
||||
|
||||
if in_ironpython:
|
||||
try:
|
||||
from ironpython_keysyms import *
|
||||
success=True
|
||||
except ImportError,x:
|
||||
success = True
|
||||
except ImportError, x:
|
||||
raise
|
||||
else:
|
||||
try:
|
||||
from keysyms import *
|
||||
success=True
|
||||
except ImportError,x:
|
||||
success = True
|
||||
except ImportError, x:
|
||||
pass
|
||||
|
||||
if not success:
|
||||
raise ImportError(u"Could not import keysym for local pythonversion",x)
|
||||
raise ImportError(u"Could not import keysym for local pythonversion", x)
|
||||
@@ -15,67 +15,68 @@ except NameError:
|
||||
|
||||
from pyreadline.unicode_helper import ensure_unicode
|
||||
|
||||
validkey =set([u'cancel', u'backspace', u'tab', u'clear',
|
||||
u'return', u'shift_l', u'control_l', u'alt_l',
|
||||
u'pause', u'caps_lock', u'escape', u'space',
|
||||
u'prior', u'next', u'end', u'home',
|
||||
u'left', u'up', u'right', u'down',
|
||||
u'select', u'print', u'execute', u'snapshot',
|
||||
u'insert', u'delete', u'help', u'f1',
|
||||
u'f2', u'f3', u'f4', u'f5',
|
||||
u'f6', u'f7', u'f8', u'f9',
|
||||
u'f10', u'f11', u'f12', u'f13',
|
||||
u'f14', u'f15', u'f16', u'f17',
|
||||
u'f18', u'f19', u'f20', u'f21',
|
||||
u'f22', u'f23', u'f24', u'num_lock',
|
||||
u'scroll_lock', u'vk_apps', u'vk_processkey',u'vk_attn',
|
||||
u'vk_crsel', u'vk_exsel', u'vk_ereof', u'vk_play',
|
||||
u'vk_zoom', u'vk_noname', u'vk_pa1', u'vk_oem_clear',
|
||||
u'numpad0', u'numpad1', u'numpad2', u'numpad3',
|
||||
u'numpad4', u'numpad5', u'numpad6', u'numpad7',
|
||||
u'numpad8', u'numpad9', u'divide', u'multiply',
|
||||
u'add', u'subtract', u'vk_decimal'])
|
||||
validkey =set([u'cancel', u'backspace', u'tab', u'clear',
|
||||
u'return', u'shift_l', u'control_l', u'alt_l',
|
||||
u'pause', u'caps_lock', u'escape', u'space',
|
||||
u'prior', u'next', u'end', u'home',
|
||||
u'left', u'up', u'right', u'down',
|
||||
u'select', u'print', u'execute', u'snapshot',
|
||||
u'insert', u'delete', u'help', u'f1',
|
||||
u'f2', u'f3', u'f4', u'f5',
|
||||
u'f6', u'f7', u'f8', u'f9',
|
||||
u'f10', u'f11', u'f12', u'f13',
|
||||
u'f14', u'f15', u'f16', u'f17',
|
||||
u'f18', u'f19', u'f20', u'f21',
|
||||
u'f22', u'f23', u'f24', u'num_lock',
|
||||
u'scroll_lock', u'vk_apps', u'vk_processkey',u'vk_attn',
|
||||
u'vk_crsel', u'vk_exsel', u'vk_ereof', u'vk_play',
|
||||
u'vk_zoom', u'vk_noname', u'vk_pa1', u'vk_oem_clear',
|
||||
u'numpad0', u'numpad1', u'numpad2', u'numpad3',
|
||||
u'numpad4', u'numpad5', u'numpad6', u'numpad7',
|
||||
u'numpad8', u'numpad9', u'divide', u'multiply',
|
||||
u'add', u'subtract', u'vk_decimal'])
|
||||
|
||||
escape_sequence_to_special_key={u"\\e[a":u"up", u"\\e[b":u"down", u"del":u"delete"}
|
||||
escape_sequence_to_special_key = {u"\\e[a" : u"up", u"\\e[b" : u"down", u"del" : u"delete"}
|
||||
|
||||
class KeyPress(object):
|
||||
def __init__(self,char="",shift=False,control=False,meta=False,keyname=u""):
|
||||
def __init__(self, char="", shift=False, control=False, meta=False,keyname=u""):
|
||||
if control or meta or shift:
|
||||
char=char.upper()
|
||||
self.info=dict(char=char,
|
||||
shift=shift,
|
||||
control=control,
|
||||
meta=meta,
|
||||
keyname=keyname)
|
||||
char = char.upper()
|
||||
self.info = dict(char=char,
|
||||
shift=shift,
|
||||
control=control,
|
||||
meta=meta,
|
||||
keyname=keyname)
|
||||
|
||||
def create(name):
|
||||
def get(self):
|
||||
return self.info[name]
|
||||
def set(self,value):
|
||||
self.info[name]=value
|
||||
return property(get,set)
|
||||
char=create(u"char")
|
||||
shift=create(u"shift")
|
||||
control=create(u"control")
|
||||
meta=create(u"meta")
|
||||
keyname=create(u"keyname")
|
||||
|
||||
def set(self, value):
|
||||
self.info[name] = value
|
||||
return property(get, set)
|
||||
char = create(u"char")
|
||||
shift = create(u"shift")
|
||||
control = create(u"control")
|
||||
meta = create(u"meta")
|
||||
keyname = create(u"keyname")
|
||||
|
||||
def __repr__(self):
|
||||
return u"(%s,%s,%s,%s)"%tuple(map(ensure_unicode,self.tuple()))
|
||||
return u"(%s,%s,%s,%s)"%tuple(map(ensure_unicode, self.tuple()))
|
||||
|
||||
def tuple(self):
|
||||
if self.keyname:
|
||||
return (self.control,self.meta,self.shift,self.keyname)
|
||||
return (self.control, self.meta, self.shift, self.keyname)
|
||||
else:
|
||||
if self.control or self.meta or self.shift:
|
||||
return (self.control,self.meta,self.shift,self.char.upper())
|
||||
return (self.control, self.meta, self.shift, self.char.upper())
|
||||
else:
|
||||
return (self.control,self.meta,self.shift,self.char)
|
||||
return (self.control, self.meta, self.shift, self.char)
|
||||
|
||||
def make_KeyPress_from_keydescr(keydescr):
|
||||
keyinfo=KeyPress()
|
||||
if len(keydescr)>2 and keydescr[:1]==u'"' and keydescr[-1:]==u'"':
|
||||
keydescr=keydescr[1:-1]
|
||||
keyinfo = KeyPress()
|
||||
if len(keydescr) > 2 and keydescr[:1] == u'"' and keydescr[-1:] == u'"':
|
||||
keydescr = keydescr[1:-1]
|
||||
|
||||
while 1:
|
||||
lkeyname = keydescr.lower()
|
||||
@@ -105,14 +106,14 @@ def make_KeyPress_from_keydescr(keydescr):
|
||||
else:
|
||||
if len(keydescr) > 1:
|
||||
if keydescr.strip().lower() in validkey:
|
||||
keyinfo.keyname=keydescr.strip().lower()
|
||||
keyinfo.char=""
|
||||
keyinfo.keyname = keydescr.strip().lower()
|
||||
keyinfo.char = ""
|
||||
else:
|
||||
raise IndexError(u"Not a valid key: '%s'"%keydescr)
|
||||
else:
|
||||
keyinfo.char=keydescr
|
||||
keyinfo.char = keydescr
|
||||
return keyinfo
|
||||
|
||||
if __name__==u"__main__":
|
||||
if __name__ == u"__main__":
|
||||
import startup
|
||||
|
||||
@@ -7,12 +7,12 @@
|
||||
# the file COPYING, distributed as part of this software.
|
||||
#*****************************************************************************
|
||||
import System
|
||||
from common import validkey,KeyPress,make_KeyPress_from_keydescr
|
||||
from common import validkey, KeyPress, make_KeyPress_from_keydescr
|
||||
|
||||
c32=System.ConsoleKey
|
||||
Shift=System.ConsoleModifiers.Shift
|
||||
Control=System.ConsoleModifiers.Control
|
||||
Alt=System.ConsoleModifiers.Alt
|
||||
c32 = System.ConsoleKey
|
||||
Shift = System.ConsoleModifiers.Shift
|
||||
Control = System.ConsoleModifiers.Control
|
||||
Alt = System.ConsoleModifiers.Alt
|
||||
# table for translating virtual keys to X windows key symbols
|
||||
code2sym_map = {#c32.CANCEL: u'Cancel',
|
||||
c32.Backspace: u'BackSpace',
|
||||
@@ -181,25 +181,22 @@ def keyseq_to_keyinfo(keyseq):
|
||||
return res[0]
|
||||
|
||||
def make_keyinfo(keycode, state):
|
||||
# control = (state & (4+8)) != 0
|
||||
# meta = (state & (1+2)) != 0
|
||||
# shift = (state & 0x10) != 0
|
||||
control=False
|
||||
meta=False
|
||||
shift=False
|
||||
control = False
|
||||
meta =False
|
||||
shift = False
|
||||
return (control, meta, shift, keycode)
|
||||
|
||||
|
||||
def make_KeyPress(char,state,keycode):
|
||||
def make_KeyPress(char, state, keycode):
|
||||
|
||||
shift=bool(int(state)&int(Shift))
|
||||
control=bool(int(state)&int(Control))
|
||||
meta=bool(int(state)&int(Alt))
|
||||
keyname=code2sym_map.get(keycode,u"").lower()
|
||||
shift = bool(int(state) & int(Shift))
|
||||
control = bool(int(state) & int(Control))
|
||||
meta = bool(int(state) & int(Alt))
|
||||
keyname = code2sym_map.get(keycode, u"").lower()
|
||||
if control and meta: #equivalent to altgr so clear flags
|
||||
control=False
|
||||
meta=False
|
||||
control = False
|
||||
meta = False
|
||||
elif control:
|
||||
char=str(keycode)
|
||||
return KeyPress(char,shift,control,meta,keyname)
|
||||
char = str(keycode)
|
||||
return KeyPress(char, shift, control, meta, keyname)
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ from ctypes import windll
|
||||
import ctypes
|
||||
# table for translating virtual keys to X windows key symbols
|
||||
|
||||
from common import validkey,KeyPress,make_KeyPress_from_keydescr
|
||||
from common import validkey, KeyPress, make_KeyPress_from_keydescr
|
||||
|
||||
code2sym_map = {c32.VK_CANCEL: u'cancel',
|
||||
c32.VK_BACK: u'backspace',
|
||||
@@ -117,15 +117,15 @@ def make_KeyPress(char, state, keycode):
|
||||
meta = (state & (1+2)) != 0
|
||||
shift = (state & 0x10) != 0
|
||||
if control and not meta:#Matches ctrl- chords should pass keycode as char
|
||||
char=chr(keycode)
|
||||
char = chr(keycode)
|
||||
elif control and meta: #Matches alt gr and should just pass on char
|
||||
control=False
|
||||
meta=False
|
||||
control = False
|
||||
meta = False
|
||||
try:
|
||||
keyname=code2sym_map[keycode]
|
||||
except KeyError:
|
||||
keyname=u""
|
||||
out=KeyPress(char, shift, control, meta, keyname)
|
||||
keyname = u""
|
||||
out = KeyPress(char, shift, control, meta, keyname)
|
||||
return out
|
||||
|
||||
if __name__==u"__main__":
|
||||
|
||||
Reference in New Issue
Block a user