home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2337 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  5.1 KB  |  190 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import System
  5. from common import validkey, KeyPress, make_KeyPress_from_keydescr
  6. c32 = System.ConsoleKey
  7. Shift = System.ConsoleModifiers.Shift
  8. Control = System.ConsoleModifiers.Control
  9. Alt = System.ConsoleModifiers.Alt
  10. code2sym_map = {
  11.     c32.Backspace: 'BackSpace',
  12.     c32.Tab: 'Tab',
  13.     c32.Clear: 'Clear',
  14.     c32.Enter: 'Return',
  15.     c32.Pause: 'Pause',
  16.     c32.Escape: 'Escape',
  17.     c32.PageUp: 'Prior',
  18.     c32.PageDown: 'Next',
  19.     c32.End: 'End',
  20.     c32.Home: 'Home',
  21.     c32.LeftArrow: 'Left',
  22.     c32.UpArrow: 'Up',
  23.     c32.RightArrow: 'Right',
  24.     c32.DownArrow: 'Down',
  25.     c32.Select: 'Select',
  26.     c32.Print: 'Print',
  27.     c32.Execute: 'Execute',
  28.     c32.Insert: 'Insert',
  29.     c32.Delete: 'Delete',
  30.     c32.Help: 'Help',
  31.     c32.F1: 'F1',
  32.     c32.F2: 'F2',
  33.     c32.F3: 'F3',
  34.     c32.F4: 'F4',
  35.     c32.F5: 'F5',
  36.     c32.F6: 'F6',
  37.     c32.F7: 'F7',
  38.     c32.F8: 'F8',
  39.     c32.F9: 'F9',
  40.     c32.F10: 'F10',
  41.     c32.F11: 'F11',
  42.     c32.F12: 'F12',
  43.     c32.F13: 'F13',
  44.     c32.F14: 'F14',
  45.     c32.F15: 'F15',
  46.     c32.F16: 'F16',
  47.     c32.F17: 'F17',
  48.     c32.F18: 'F18',
  49.     c32.F19: 'F19',
  50.     c32.F20: 'F20',
  51.     c32.F21: 'F21',
  52.     c32.F22: 'F22',
  53.     c32.F23: 'F23',
  54.     c32.F24: 'F24',
  55.     c32.OemClear: 'VK_OEM_CLEAR',
  56.     c32.NumPad0: 'NUMPAD0',
  57.     c32.NumPad1: 'NUMPAD1',
  58.     c32.NumPad2: 'NUMPAD2',
  59.     c32.NumPad3: 'NUMPAD3',
  60.     c32.NumPad4: 'NUMPAD4',
  61.     c32.NumPad5: 'NUMPAD5',
  62.     c32.NumPad6: 'NUMPAD6',
  63.     c32.NumPad7: 'NUMPAD7',
  64.     c32.NumPad8: 'NUMPAD8',
  65.     c32.NumPad9: 'NUMPAD9',
  66.     c32.Divide: 'Divide',
  67.     c32.Multiply: 'Multiply',
  68.     c32.Add: 'Add',
  69.     c32.Subtract: 'Subtract',
  70.     c32.Decimal: 'VK_DECIMAL' }
  71.  
  72. def make_keysym(keycode):
  73.     
  74.     try:
  75.         sym = code2sym_map[keycode]
  76.     except KeyError:
  77.         sym = ''
  78.  
  79.     return sym
  80.  
  81. sym2code_map = { }
  82. for code, sym in code2sym_map.iteritems():
  83.     sym2code_map[sym.lower()] = code
  84.  
  85.  
  86. def key_text_to_keyinfo(keytext):
  87.     if keytext.startswith('"'):
  88.         return keyseq_to_keyinfo(keytext[1:-1])
  89.     return keyname_to_keyinfo(keytext)
  90.  
  91.  
  92. def char_to_keyinfo(char, control = False, meta = False, shift = False):
  93.     vk = ord(char)
  94.     if vk & 65535 == 65535:
  95.         print 'VkKeyScan("%s") = %x' % (char, vk)
  96.         raise ValueError, 'bad key'
  97.     vk & 65535 == 65535
  98.     if vk & 256:
  99.         shift = True
  100.     
  101.     if vk & 512:
  102.         control = True
  103.     
  104.     if vk & 1024:
  105.         meta = True
  106.     
  107.     return (control, meta, shift, vk & 255)
  108.  
  109.  
  110. def keyname_to_keyinfo(keyname):
  111.     control = False
  112.     meta = False
  113.     shift = False
  114.     while None:
  115.         lkeyname = keyname.lower()
  116.         if lkeyname.startswith('control-'):
  117.             control = True
  118.             keyname = keyname[8:]
  119.             continue
  120.         if lkeyname.startswith('ctrl-'):
  121.             control = True
  122.             keyname = keyname[5:]
  123.             continue
  124.         if lkeyname.startswith('meta-'):
  125.             meta = True
  126.             keyname = keyname[5:]
  127.             continue
  128.         if lkeyname.startswith('alt-'):
  129.             meta = True
  130.             keyname = keyname[4:]
  131.             continue
  132.         if lkeyname.startswith('shift-'):
  133.             shift = True
  134.             keyname = keyname[6:]
  135.             continue
  136.         if len(keyname) > 1:
  137.             return (control, meta, shift, sym2code_map.get(keyname.lower(), ' '))
  138.         return char_to_keyinfo(keyname, control, meta, shift)
  139.         continue
  140.         return None
  141.  
  142.  
  143. def keyseq_to_keyinfo(keyseq):
  144.     res = []
  145.     control = False
  146.     meta = False
  147.     shift = False
  148.     while keyseq.startswith('\\C-'):
  149.         control = True
  150.         keyseq = keyseq[3:]
  151.         continue
  152.         if keyseq.startswith('\\M-'):
  153.             meta = True
  154.             keyseq = keyseq[3:]
  155.             continue
  156.     if keyseq.startswith('\\e'):
  157.         res.append(char_to_keyinfo('\x1b', control, meta, shift))
  158.         control = meta = shift = False
  159.         keyseq = keyseq[2:]
  160.         continue
  161.     if len(keyseq) >= 1:
  162.         res.append(char_to_keyinfo(keyseq[0], control, meta, shift))
  163.         control = meta = shift = False
  164.         keyseq = keyseq[1:]
  165.         continue
  166.     return res[0]
  167.     continue
  168.  
  169.  
  170. def make_keyinfo(keycode, state):
  171.     control = False
  172.     meta = False
  173.     shift = False
  174.     return (control, meta, shift, keycode)
  175.  
  176.  
  177. def make_KeyPress(char, state, keycode):
  178.     shift = bool(int(state) & int(Shift))
  179.     control = bool(int(state) & int(Control))
  180.     meta = bool(int(state) & int(Alt))
  181.     keyname = code2sym_map.get(keycode, '').lower()
  182.     if control and meta:
  183.         control = False
  184.         meta = False
  185.     elif control:
  186.         char = str(keycode)
  187.     
  188.     return KeyPress(char, shift, control, meta, keyname)
  189.  
  190.