How to use toKeyCode method in fMBT

Best Python code snippet using fMBT_python

__init__.py

Source:__init__.py Github

copy

Full Screen

1import time2import keycode3import pygame4import Quartz5from lemur.controller import XBox360 as XBox360Base6from lemur import Tickable7"""8Buttons:90 up101 down112 left123 right134 start145 back156 leftStick167 rightStick178 leftBumper189 rightBumper1910 xbox2011 a2112 b2213 x2314 y24Axes:250 leftStickX -1 (left) 1 (right)261 leftStickY -1 (up) 1 (down)272 rightStickX -1 (left) 1 (right)283 rightStickY -1 (up) 1 (down)294 leftTrigger 1 (pressed) -1 (released)305 rightTrigger 1 (pressed) -1 (released)31"""32DISCONNECT_THRESHOLD = 1 # seconds33class MockJoystick(Tickable):34 def get_button(self, i):35 return False36 def get_axis(self, i):37 return 038class XBox360Source(Tickable):39 def __init__(self, idx=0):40 self.idx = idx41 pygame.init()42 self.mock_joy = MockJoystick()43 self.joy = self.mock_joy44 self.last_update = 045 def tick(self):46 now = time.time()47 for event in pygame.event.get():48 if hasattr(event, 'joy'):49 self.last_update = now50 if now - self.last_update > DISCONNECT_THRESHOLD:51 #print 'Checking for joystick disconnect'52 pygame.joystick.quit()53 self.joy = self.mock_joy54 pygame.joystick.init()55 self.last_update = now56 disconnect_check = True57 else:58 disconnect_check = False59 pygame.event.pump()60 if pygame.joystick.get_count() > self.idx:61 if self.joy is self.mock_joy:62 #print 'Joystick connected'63 self.joy = pygame.joystick.Joystick(self.idx)64 self.joy.init()65 if disconnect_check and self.joy is self.mock_joy:66 #print 'Joystick disconnected'67 pass68 @property69 def up(self):70 return self.joy.get_button(0)71 @property72 def down(self):73 return self.joy.get_button(1)74 @property75 def left(self):76 return self.joy.get_button(2)77 @property78 def right(self):79 return self.joy.get_button(3)80 @property81 def start(self):82 return self.joy.get_button(4)83 @property84 def back(self):85 return self.joy.get_button(5)86 @property87 def leftThumb(self):88 return self.joy.get_button(6)89 @property90 def rightThumb(self):91 return self.joy.get_button(7)92 @property93 def leftShoulder(self):94 return self.joy.get_button(8)95 @property96 def rightShoulder(self):97 return self.joy.get_button(9)98 @property99 def xbox(self):100 return self.joy.get_button(10)101 @property102 def a(self):103 return self.joy.get_button(11)104 @property105 def b(self):106 return self.joy.get_button(12)107 @property108 def x(self):109 return self.joy.get_button(13)110 @property111 def y(self):112 return self.joy.get_button(14)113 @property114 def leftStickX(self):115 return self.joy.get_axis(0)116 @property117 def leftStickY(self):118 return -self.joy.get_axis(1)119 @property120 def rightStickX(self):121 return self.joy.get_axis(2)122 @property123 def rightStickY(self):124 return -self.joy.get_axis(3)125 @property126 def leftTrigger(self):127 return self.joy.get_axis(4)128 @property129 def rightTrigger(self):130 return self.joy.get_axis(5)131class XBox360(XBox360Base):132 def __init__(self, idx):133 super(XBox360, self).__init__(XBox360Source(idx))134 def tick(self):135 self.source.tick()136 super(XBox360, self).tick()137class Key(object):138 A = keycode.tokeycode('a')139 B = keycode.tokeycode('b')140 C = keycode.tokeycode('c')141 D = keycode.tokeycode('d')142 E = keycode.tokeycode('e')143 F = keycode.tokeycode('f')144 G = keycode.tokeycode('g')145 H = keycode.tokeycode('h')146 I = keycode.tokeycode('i')147 J = keycode.tokeycode('j')148 K = keycode.tokeycode('k')149 L = keycode.tokeycode('l')150 M = keycode.tokeycode('m')151 N = keycode.tokeycode('n')152 O = keycode.tokeycode('o')153 P = keycode.tokeycode('p')154 Q = keycode.tokeycode('q')155 R = keycode.tokeycode('r')156 S = keycode.tokeycode('s')157 T = keycode.tokeycode('t')158 U = keycode.tokeycode('u')159 V = keycode.tokeycode('v')160 W = keycode.tokeycode('w')161 X = keycode.tokeycode('x')162 Y = keycode.tokeycode('y')163 Z = keycode.tokeycode('z')164 D1 = keycode.tokeycode('1')165 D2 = keycode.tokeycode('2')166 D3 = keycode.tokeycode('3')167 D4 = keycode.tokeycode('4')168 D5 = keycode.tokeycode('5')169 D6 = keycode.tokeycode('6')170 D7 = keycode.tokeycode('7')171 D8 = keycode.tokeycode('8')172 D9 = keycode.tokeycode('9')173 D0 = keycode.tokeycode('0')174 Backslash = keycode.tokeycode('\\')175 Space = keycode.tokeycode(' ')176 Grave = keycode.tokeycode('`')177 Minus = keycode.tokeycode('-')178 Equals = keycode.tokeycode('=')179 Apostrophe = keycode.tokeycode("'")180 Tab = keycode.tokeycode("\t") # 0x30181 #NumberLock182 NumberPadPeriod = 0x41183 LeftShift = 0x38184 Return = 0x24,185 #Tab = 0x30,186 Space = 0x31,187 Delete = 0x33,188 Escape = 0x35,189 Command = 0x37,190 Shift = 0x38,191 CapsLock = 0x39,192 Option = 0x3A,193 Control = 0x3B,194 RightShift = 0x3C,195 RightOption = 0x3D,196 RightControl = 0x3E,197 Function = 0x3F,198class Keyboard(object):199 KEY_FLAGS = {200 Key.LeftShift: Quartz.kCGEventFlagMaskShift,201 Key.RightShift: Quartz.kCGEventFlagMaskShift,202 Key.Command: Quartz.kCGEventFlagMaskCommand,203 Key.Option: Quartz.kCGEventFlagMaskAlternate,204 Key.Control: Quartz.kCGEventFlagMaskControl,205 # apparently not needed....why is this one handled automatically but the other not?206 #Key.CapsLock: Quartz.kCGEventFlagMaskAlphaShift,207 }208 def __init__(self):209 self.flags = 0210 self.unhandled_flags = 0xFFFFFFFFFFFFFFFF211 for val in Keyboard.KEY_FLAGS.values():212 self.unhandled_flags &= ~val213 #TODO: LeftShift not working214 def keyboard_event(self, keycode, down):215 # TODO: do we need to generate the source every time?216 source = Quartz.CGEventSourceCreate(Quartz.kCGEventSourceStateCombinedSessionState)217 # should we do this before or after the key event?218 if keycode in Keyboard.KEY_FLAGS:219 if down:220 self.flags |= Keyboard.KEY_FLAGS[keycode]221 else:222 self.flags &= ~Keyboard.KEY_FLAGS[keycode]223 event = Quartz.CGEventCreateKeyboardEvent(source, keycode, down)224 Quartz.CGEventSetFlags(event, self.flags | (Quartz.CGEventGetFlags(event) & self.unhandled_flags))225 Quartz.CGEventPost(Quartz.kCGAnnotatedSessionEventTap, event)226 def setKeyDown(self, key):227 self.keyboard_event(key, True)228 def setKeyUp(self, key):229 self.keyboard_event(key, False)230class Mouse(Tickable):231 def __init__(self):232 self.posx = 0233 self.posy = 0234 self.leftDown = False235 self.rightDown = False236 self._deltaX = 0237 self._deltaY = 0238 #TODO: when the mouse if disassociated from the cursor we shouldn't be updating the position239 def mouse_event(self, event_type, button, move=False):240 event = Quartz.CGEventCreateMouseEvent(241 None,242 event_type,243 (self.posx, self.posy),244 button)245 Quartz.CGEventSetType(event, event_type)246 if move:247 Quartz.CGEventSetIntegerValueField(event, Quartz.kCGMouseEventDeltaX, int(self._deltaX))248 Quartz.CGEventSetIntegerValueField(event, Quartz.kCGMouseEventDeltaY, int(self._deltaY))249 Quartz.CGEventPost(Quartz.kCGHIDEventTap, event)250 def setButton(self, idx, down):251 if idx == 0:252 self.leftDown = down253 elif idx == 1:254 self.rightDown = down255 self.mouse_event(256 (Quartz.kCGEventLeftMouseDown if down else Quartz.kCGEventLeftMouseUp) if idx == 0257 else (Quartz.kCGEventRightMouseDown if down else Quartz.kCGEventRightMouseUp),258 Quartz.kCGMouseButtonLeft if idx == 0 else Quartz.kCGMouseButtonRight259 )260 def mouse_moved(self):261 e = Quartz.CGEventCreate(None)262 pos = Quartz.CGEventGetLocation(e)263 (self.posx, self.posy) = (pos.x, pos.y)264 self._deltaX *= 4265 self._deltaY *= 4266 self.posx += self._deltaX267 self.posx = max(0, self.posx)268 self.posy += self._deltaY269 self.posy = max(0, self.posy)270 self.mouse_event(271 Quartz.kCGEventLeftMouseDragged if self.leftDown272 else (Quartz.kCGEventRightMouseDragged if self.rightDown273 else Quartz.kCGEventMouseMoved),274 Quartz.kCGMouseButtonRight if self.rightDown else Quartz.kCGMouseButtonLeft,275 True)276 self._deltaX = 0277 self._deltaY = 0278 @property279 def deltaX(self):280 return self._deltaX281 @deltaX.setter282 def deltaX(self, value):283 self._deltaX = value284 @property285 def deltaY(self):286 return self._deltaY287 @deltaY.setter288 def deltaY(self, value):289 self._deltaY = value290 def tick(self):291 if self._deltaX == 0 and self._deltaY == 0:292 return...

Full Screen

Full Screen

keyEvent.py

Source:keyEvent.py Github

copy

Full Screen

...6# From http://stackoverflow.com/questions/281133/controlling-the-mouse-from-python-in-os-x7# and from https://developer.apple.com/library/mac/documentation/Carbon/Reference/QuartzEventServicesRef/index.html#//apple_ref/c/func/CGEventCreateKeyboardEvent8class KeyEvent():9 def KeyDown(k):10 keyCode, shiftKey = toKeyCode(k)11 time.sleep(0.0001)12 if shiftKey:13 CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, 0x38, True))14 time.sleep(0.0001)15 CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, keyCode, True))16 time.sleep(0.0001)17 if shiftKey:18 CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, 0x38, False))19 time.sleep(0.0001)20 def KeyUp(k):21 keyCode, shiftKey = toKeyCode(k)22 time.sleep(0.0001)23 CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, keyCode, False))24 time.sleep(0.0001)25 def KeyPress(k):26 keyCode, shiftKey = toKeyCode(k)27 time.sleep(0.0001)28 if shiftKey:29 CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, 0x38, True))30 time.sleep(0.0001)31 CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, keyCode, True))32 time.sleep(0.0001)33 CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, keyCode, False))34 time.sleep(0.0001)35 if shiftKey:36 CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, 0x38, False))37 time.sleep(0.0001)38 # From http://stackoverflow.com/questions/3202629/where-can-i-find-a-list-of-mac-virtual-key-codes39 def toKeyCode(c):40 shiftKey = False41 # Letter42 if c.isalpha():43 if not c.islower():44 shiftKey = True45 c = c.lower()46 if c in shiftChars:47 shiftKey = True48 c = shiftChars[c]49 if c in keyCodeMap:50 keyCode = keyCodeMap[c]51 else:52 keyCode = ord(c)53 return keyCode, shiftKey...

Full Screen

Full Screen

keyboard.py

Source:keyboard.py Github

copy

Full Screen

...156 'down' : 0x7D,157 'up' : 0x7E158 }159 # See: https://stackoverflow.com/q/3202629/55075160 # def toKeyCode(self, c):161 # shiftKey = False162 # # Letter163 # if c.isalpha():164 # if not c.islower():165 # shiftKey = False166 # c = c.lower()167 # if c in Keyboard.shiftChars:168 # shiftKey = True169 # c = Keyboard.shiftChars[c]170 # if c in Keyboard.keyCodeMap:171 # keyCode = Keyboard.keyCoxdeMap[c]172 # else:173 # keyCode = ord(c)174 # return keyCode, shiftKey175 def toKeyCode(self, c):176 if c in Keyboard.keyCodeMap:177 keyCode = Keyboard.keyCodeMap[c]178 else:179 keyCode = ord(c)180 return keyCode181 def KeyDown(self, k):182 keyCode = self.toKeyCode(k)183 CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, keyCode, True))184 time.sleep(0.00001)185 def KeyUp(self, k, force=False):186 keyCode = self.toKeyCode(k)187 CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, keyCode, False))188 time.sleep(0.00001)189 def KeyPress(self, k):190 keyCode = self.toKeyCode(k)191 CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, keyCode, True))192 time.sleep(0.00001)193 CGEventPost(kCGHIDEventTap, CGEventCreateKeyboardEvent(None, keyCode, False))194 time.sleep(0.00001)195 def Type(self, text):196 for key in text:197 self.KeyDown(key)198 self.KeyUp(key)199keyboard = Keyboard()200if __name__ == "__main__":201 for key in ['up', 'down', 'up', 'down']:202 time.sleep(1)203 keyboard.KeyDown(key)204 time.sleep(1)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run fMBT automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful