def handle_ctrl_c(self): from pyreadline.keysyms.common import KeyPress from pyreadline.console.event import Event log_sock("KBDIRQ") event = Event(0, 0) event.char = "c" event.keyinfo = KeyPress("c", shift=False, control=True, meta=False, keyname=None) if self.allow_ctrl_c: now = time.time() if (now - self.ctrl_c_timeout) < self.ctrl_c_tap_time_interval: log_sock("Raise KeyboardInterrupt") raise KeyboardInterrupt else: self.ctrl_c_timeout = now else: raise KeyboardInterrupt dispatch_func = self.key_dispatch.get(event.keyinfo.tuple(), None) if dispatch_func: r = dispatch_func(event) self._keylog(dispatch_func, self.l_buffer) self.l_buffer.push_undo()
def keytext_to_keyinfo_and_event (keytext): keyinfo = KeyPress.from_keydescr(keytext) if len(keytext) == 3 and keytext[0] == '"' and keytext[2] == '"': event = Event (keytext[1]) else: event = Event (keyinfo.to_tuple() [3]) return keyinfo, event
def _readline_from_keyboard(self): c = self.console def nop(e): pass while 1: self._update_line() try: event = c.getkeypress() except KeyboardInterrupt: from pyreadline.keysyms.common import KeyPress from pyreadline.console.event import Event event = Event(0, 0) event.char = "c" event.keyinfo = KeyPress("c", shift=False, control=True, meta=False, keyname=None) log_sock("KBDIRQ") if self.allow_ctrl_c: now = time.time() if (now - self.ctrl_c_timeout ) < self.ctrl_c_tap_time_interval: raise else: self.ctrl_c_timeout = now pass else: raise if self.next_meta: self.next_meta = False control, meta, shift, code = event.keyinfo event.keyinfo = (control, True, shift, code) #Process exit keys. Only exit on empty line keyinfo = event.keyinfo.tuple() if keyinfo in self.exit_dispatch: if lineobj.EndOfLine(self.l_buffer) == 0: raise EOFError if len(keyinfo[-1]) > 1: default = nop else: default = self.self_insert dispatch_func = self.key_dispatch.get(keyinfo, default) log("readline from keyboard:%s,%s" % (keyinfo, dispatch_func)) log_sock("%s|%s" % (format(keyinfo), dispatch_func.__name__), "bound_function") r = None if dispatch_func: r = dispatch_func(event) self._keylog(dispatch_func, self.l_buffer) self.l_buffer.push_undo() self.previous_func = dispatch_func if r: self._update_line() break
def handle_ctrl_c(self): from pyreadline.keysyms.common import KeyPress from pyreadline.console.event import Event log("KBDIRQ") event = Event(0, 0) event.char = "c" event.keyinfo = KeyPress("c", shift=False, control=True, meta=False, keyname=None) if self.allow_ctrl_c: now = time.time() if (now - self.ctrl_c_timeout) < self.ctrl_c_tap_time_interval: log("Raise KeyboardInterrupt") raise KeyboardInterrupt else: self.ctrl_c_timeout = now else: if self.line_end_pos: self.console.pos(self.line_end_pos[0], self.line_end_pos[1]) self.console.clear_state() self.mode.clear_state() raise KeyboardInterrupt return event
def make_KeyPress(char, state, keycode): control = (state & (4 + 8)) != 0 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) elif control and meta: # Matches alt gr and should just pass on char control = False meta = False try: keyname = code2sym_map[keycode] except KeyError: keyname = "" out = KeyPress(char, shift, control, meta, keyname) return out
def KeyPress_from_event(event): keysym=event.keysym.lower() char=event.char if keysym in translate: keysym=translate[keysym] shift=event.state&1!=0 control=event.state&4!=0 meta=event.state&(131072)!=0 if len(keysym)==1 and control and meta: keysym="" elif len(keysym)==1: char=keysym keysym="" return KeyPress(char, shift, control, meta, keysym)
def __init__(self, console, input): '''Initialize an event from the Windows input structure.''' self.type = '??' self.serial = console.next_serial() self.width = 0 self.height = 0 self.x = 0 self.y = 0 self.char = '' self.keycode = 0 self.keysym = '??' self.keyinfo = None # a tuple with (control, meta, shift, keycode) for dispatch self.width = None if input.EventType == KEY_EVENT: if input.Event.KeyEvent.bKeyDown: self.type = "KeyPress" else: self.type = "KeyRelease" self.char = input.Event.KeyEvent.uChar.UnicodeChar self.keycode = input.Event.KeyEvent.wVirtualKeyCode self.state = input.Event.KeyEvent.dwControlKeyState self.keyinfo = KeyPress.from_char_state_code(self.char,self.state,self.keycode) elif input.EventType == MOUSE_EVENT: if input.Event.MouseEvent.dwEventFlags & MOUSE_MOVED: self.type = "Motion" else: self.type = "Button" self.x = input.Event.MouseEvent.dwMousePosition.X self.y = input.Event.MouseEvent.dwMousePosition.Y self.state = input.Event.MouseEvent.dwButtonState elif input.EventType == WINDOW_BUFFER_SIZE_EVENT: self.type = "Configure" self.width = input.Event.WindowBufferSizeEvent.dwSize.X self.height = input.Event.WindowBufferSizeEvent.dwSize.Y elif input.EventType == FOCUS_EVENT: if input.Event.FocusEvent.bSetFocus: self.type = "FocusIn" else: self.type = "FocusOut" elif input.EventType == MENU_EVENT: self.type = "Menu" self.state = input.Event.MenuEvent.dwCommandId
def char_to_keyinfo(char, control=False, meta=False, shift=False): k = KeyPress() vk = VkKeyScan(ord(char)) if vk & 0xFFFF == 0xFFFF: print('VkKeyScan("%s") = %x' % (char, vk)) raise ValueError("bad key") if vk & 0x100: k.shift = True if vk & 0x200: k.control = True if vk & 0x400: k.meta = True k.char = chr(vk & 0xFF) return k
def handle_ctrl_c(self): from pyreadline.keysyms.common import KeyPress from pyreadline.console.event import Event log('KBDIRQ') event = Event(0, 0) event.char = 'c' event.keyinfo = KeyPress('c', shift=False, control=True, meta=False, keyname=None) if self.allow_ctrl_c: now = time.time() if now - self.ctrl_c_timeout < self.ctrl_c_tap_time_interval: log('Raise KeyboardInterrupt') raise KeyboardInterrupt else: self.ctrl_c_timeout = now else: raise KeyboardInterrupt return event
def un_bind_exit_key(key): keyinfo = KeyPress.from_keydescr(key).tuple() if keyinfo in modes[mode].exit_dispatch: del modes[mode].exit_dispatch[keyinfo]