示例#1
0
文件: gui.py 项目: chrisnorman7/oht
 def press_current_key(self, t = None):
  """Push the current key."""
  if not t or time() - t >= application.config.get('settings', 'timeout'):
   try:
    k = self.key_value
    self.key_value = None
    self.last_key = None
    self.last_key_time = None
   except wx.PyDeadObjectError:
    return # The frame has been deleted.
   if k:
    if application.config.get('sounds', 'keyboard'):
     s = Sound('type')
     s.Play()
    keys = []
    try:
     shift = modes.shift_modes.index(application.config.get('settings', 'shift_mode'))
    except ValueError: # The value got mangled.
     shift = 0
     application.config.set('settings', 'shift_mode', modes.shift_modes[0])
    if shift != modes.MODE_SHIFT_CAPSLOCK:
     application.config.set('settings', 'shift_mode', modes.shift_modes[0])
    if shift in [modes.MODE_SHIFT_UPPER, modes.MODE_SHIFT_CAPSLOCK] or (self.autocapitalise and k in letters):
     if application.config.get('sounds', 'capslock'):
      s = Sound('capslock')
      s.Play()
     keys.insert(0, 'shift')
    elif shift == modes.MODE_SHIFT_CTRL: # Control key.
     keys.insert(0, 'ctrl')
    elif shift == modes.MODE_SHIFT_ALT:
     keys.insert(0, 'alt')
    keys.append(k)
    if k in application.config.get('settings', 'autocapitalise'):
     self.autocapitalise = True
    elif k == 'spacebar':
     self.autocapitalise = self.autocapitalise
    elif k in application.special_keys:
     keys = ['shift', application.special_keys[k]]
    else:
     self.autocapitalise = False
    self.all_keys += keys
    for item in self.all_keys:
     try:
      release(item)
     except KeyError:
      pass # Invalid character.
    self.all_keys = []
    try:
     pressHoldRelease(*keys)
    except KeyError as e:
     wx.MessageBox('Failed to type character: %s. Consider adding it to special keys.' % e.message, 'Error')
示例#2
0
文件: gui.py 项目: chrisnorman7/oht
 def handle_hotkey(self, key):
  """Does the actual handling in a platform-independant way."""
  if self.key_timer:
   self.key_timer.cancel()
  key = key.lower()
  mode = modes.get_current_mode()
  if key == 'subtract':
   get_menu()
  elif key in ['divide', 'decimal']:
   modes.switch_mode({'divide': 'operation', 'decimal': 'shift'}[key])
   if key == 'decimal':
    self.autocapitalise = False
  elif key == 'add':
   if mode in [modes.MODE_OPERATION_STANDARD, modes.MODE_OPERATION_NUMBERS]:
    self.press_current_key()
    press('backspace')
   else:
    pressHoldRelease(application.config.get(mode.name, key, 'alt'))
  elif key == 'multiply':
   self.press_current_key()
  else:
   if mode == modes.MODE_OPERATION_NUMBERS: # Pass numbers straight through.
    press(key[-1])
   elif mode == modes.MODE_OPERATION_STANDARD:
    if key == 'enter':
     self.key_value = 'enter'
    elif key == self.last_key:
     self.index += 1
    else:
     self.press_current_key()
     self.index = 0
    possible_keys = application.config.get('keys', key)
    if self.index >= len(possible_keys):
     self.index = 0
    self.key_value = possible_keys[self.index]
    if self.key_value == ' ': # A space must be converted before sending.
     self.key_value = 'spacebar'
    output.output(self.key_value)
   else:
    pressHoldRelease(application.config.get(mode.name, key))
  self.last_key = key
  if mode == modes.MODE_OPERATION_STANDARD:
   t = time()
   self.key_timer = Timer(application.config.get('settings', 'timeout'), self.press_current_key, args = [t])
   self.key_timer.start()
   self.last_key_time = t