Package pyjamas :: Package ui :: Module KeyboardListener
[hide private]
[frames] | no frames]

Source Code for Module pyjamas.ui.KeyboardListener

  1  # Copyright 2006 James Tauber and contributors 
  2  # Copyright (C) 2009 Luke Kenneth Casson Leighton <lkcl@lkcl.net> 
  3  # 
  4  # Licensed under the Apache License, Version 2.0 (the "License"); 
  5  # you may not use this file except in compliance with the License. 
  6  # You may obtain a copy of the License at 
  7  # 
  8  #     http://www.apache.org/licenses/LICENSE-2.0 
  9  # 
 10  # Unless required by applicable law or agreed to in writing, software 
 11  # distributed under the License is distributed on an "AS IS" BASIS, 
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 13  # See the License for the specific language governing permissions and 
 14  # limitations under the License. 
 15  from pyjamas import DOM 
 16  from pyjamas.ui import Event 
 17   
 18   
 19  KEY_ALT = 18 
 20  KEY_BACKSPACE = 8 
 21  KEY_CTRL = 17 
 22  KEY_DELETE = 46 
 23  KEY_DOWN = 40 
 24  KEY_END = 35 
 25  KEY_ENTER = 13 
 26  KEY_ESCAPE = 27 
 27  KEY_HOME = 36 
 28  KEY_LEFT = 37 
 29  KEY_PAGEDOWN = 34 
 30  KEY_PAGEUP = 33 
 31  KEY_RIGHT = 39 
 32  KEY_SHIFT = 16 
 33  KEY_TAB = 9 
 34  KEY_UP = 38 
 35   
 36  KEYS = [ 
 37      KEY_ALT, 
 38      KEY_BACKSPACE, 
 39      KEY_CTRL, 
 40      KEY_DELETE, 
 41      KEY_DOWN, 
 42      KEY_END, 
 43      KEY_ENTER, 
 44      KEY_ESCAPE, 
 45      KEY_HOME, 
 46      KEY_LEFT, 
 47      KEY_PAGEDOWN, 
 48      KEY_PAGEUP, 
 49      KEY_RIGHT, 
 50      KEY_SHIFT, 
 51      KEY_TAB, 
 52      KEY_UP, 
 53  ] 
 54   
 55  MODIFIER_ALT = 4 
 56  MODIFIER_CTRL = 2 
 57  MODIFIER_SHIFT = 1 
 58   
59 -def getKeyboardModifiers(event):
60 shift = 0 61 ctrl = 0 62 alt = 0 63 64 if DOM.eventGetShiftKey(event): 65 shift = MODIFIER_SHIFT 66 67 if DOM.eventGetCtrlKey(event): 68 ctrl = MODIFIER_CTRL 69 70 if DOM.eventGetAltKey(event): 71 alt = MODIFIER_ALT 72 73 return shift | ctrl | alt
74 75
76 -def fireKeyboardEvent(listeners, sender, event):
77 modifiers = getKeyboardModifiers(event) 78 keycode = DOM.eventGetKeyCode(event) 79 80 type = DOM.eventGetType(event) 81 if type == "keydown": 82 for listener in listeners: 83 listener.onKeyDown(sender, keycode, modifiers) 84 elif type == "keyup": 85 for listener in listeners: 86 listener.onKeyUp(sender, keycode, modifiers) 87 elif type == "keypress": 88 for listener in listeners: 89 listener.onKeyPress(sender, keycode, modifiers)
90 91 KEYBOARD_EVENTS = ["keydown", "keyup", "keypress"] 92 93
94 -class KeyboardHandler:
95
96 - def __init__(self):
97 98 self._keyboardListeners = [] 99 self.sinkEvents( Event.KEYEVENTS )
100
101 - def onBrowserEvent(self, event):
102 type = DOM.eventGetType(event) 103 if type in [ "keydown", "keyup", "keypress" ]: 104 fireKeyboardEvent(self._keyboardListeners, self, event)
105
106 - def addKeyboardListener(self, listener):
107 self._keyboardListeners.append(listener)
108
109 - def removeKeyboardListener(self, listener):
110 self._keyboardListeners.remove(listener)
111
112 - def onKeyDown(self, sender, keycode, modifiers):
113 pass
114
115 - def onKeyUp(self, sender, keycode, modifiers):
116 pass
117
118 - def onKeyPress(self, sender, keycode, modifiers):
119 pass
120