How to use toButtonCode method in fMBT

Best Python code snippet using fMBT_python

fmbtuinput.py

Source:fmbtuinput.py Github

copy

Full Screen

...505 if keyCode in keyCodesInv:506 return keyCodesInv[keyCode]507 else:508 raise ValueError('Invalid keycode "%s"' % (keyCode,))509def toButtonCode(buttonCodeOrName):510 if isinstance(buttonCodeOrName, str):511 buttonCode = toKeyCode(buttonCodeOrName)512 elif buttonCodeOrName < 0xf:513 buttonCode = keyCodes["BTN_MOUSE"] + buttonCodeOrName514 else:515 buttonCode = buttonCodeOrName516 return buttonCode517def refreshDeviceInfo():518 global _g_devices519 global _g_deviceNames520 global _g_filenames521 _g_devices = file("/proc/bus/input/devices").read().split("\n\n")522 _g_deviceNames = {}523 _g_filenames = {}524 for d in _g_devices:525 if d.strip() == "":526 continue527 _name = [line.split('"')[1] for line in d.split('\n')528 if line.startswith('N: ')][0]529 _g_deviceNames[_name] = ("/dev/input/" +530 re.findall('[ =](event[0-9]+)\s', d)[0])531 _g_filenames[_g_deviceNames[_name]] = _name532def toEventFilename(deviceName):533 return _g_deviceNames[deviceName]534def toEventDeviceName(filename):535 return _g_filenames[filename]536class InputDevice(object):537 def __init__(self):538 if not "_g_devices" in globals():539 refreshDeviceInfo()540 self._fd = -1541 self._filename = None542 self._uidev = None543 self._created = False544 self._opened = False545 def __del__(self):546 if self._created:547 self.destroy()548 def startCreating(self, name, vendor, product, version,549 absmin=None, absmax=None):550 if self._fd > 0:551 raise InputDeviceError("InputDevice is already open")552 self._fd = os.open("/dev/uinput", os.O_WRONLY | os.O_NONBLOCK)553 if absmin == None:554 absmin = [0 for _ in xrange(abs_count)]555 if absmax == None:556 absmax = [0 for _ in xrange(abs_count)]557 absfuzz = [0 for _ in xrange(abs_count)]558 absflat = [0 for _ in xrange(abs_count)]559 self._uidev = struct.pack(struct_uinput_user_dev,560 name, # name561 BUS_USB, # id.bus_type562 vendor, # id.vendor563 product, # id.product564 version, # id.version565 0, # ff_effects_max566 # TODO: why absmin + absmax gives567 # error for touch?568 *(absmax + absmin + absfuzz + absflat)569 )570 def finishCreating(self):571 if self._fd < 1:572 raise InputDeviceError("startCreating() not called")573 bytes_written = os.write(self._fd, self._uidev)574 if bytes_written != sizeof_uinput_user_dev:575 raise InputDeviceError(576 "Writing to /dev/uinput failed, wrote %s/%s bytes"577 % (bytes_written, sizeof_uinput_user_dev))578 rv = fcntl.ioctl(self._fd, UI_DEV_CREATE)579 if rv != 0:580 raise InputDeviceError(581 "Creating device failed, ioctl UI_DEV_CREATE returned %s"582 % (rv,))583 self._created = True584 return True585 def destroy(self):586 if self._created:587 fcntl.ioctl(self._fd, UI_DEV_DESTROY)588 self._created = False589 self.close()590 def open(self, filename):591 if self._fd > 0:592 raise InputDeviceError("InputDevice is already open")593 if not filename.startswith("/dev/input"):594 filename = toEventFilename(filename)595 self._fd = os.open(filename, os.O_WRONLY | os.O_NONBLOCK)596 self._filename = filename597 self._created = False598 return self599 def close(self):600 if self._fd > 0:601 os.close(self._fd)602 self._fd = -1603 def filename(self):604 return self._filename605 def addCap(self, capBit, capCodeOrName, capCode2Name):606 if self._fd < 1:607 raise InputDeviceError("startCreating() not called")608 if self._created or self._opened:609 raise InputDeviceError("Cannot add capabilities after creation")610 if isinstance(capCodeOrName, int):611 capCode = capCodeOrName612 elif capCodeOrName in capCode2Name:613 capCode = capCode2Name[capCodeOrName]614 else:615 raise InputDeviceError('Unknown name "%s"' % (capCodeOrName,))616 return fcntl.ioctl(self._fd, capBit, capCode)617 def addEvent(self, eventCodeOrName):618 return self.addCap(UI_SET_EVBIT, eventCodeOrName, eventTypes)619 def addKey(self, keyCodeOrName):620 return self.addCap(UI_SET_KEYBIT, keyCodeOrName, keyCodes)621 def addRel(self, relCodeOrName):622 return self.addCap(UI_SET_RELBIT, relCodeOrName, relCodes)623 def addAbs(self, absCodeOrName):624 return self.addCap(UI_SET_ABSBIT, absCodeOrName, absCodes)625 def send(self, type_, code, value):626 if self._fd < 1:627 raise InputDeviceError("InputDevice is not open")628 if isinstance(type_, str):629 typeCode = eventTypes[type_]630 else:631 typeCode = type_632 if isinstance(code, str):633 codeCode = event_codetables[typeCode][code]634 else:635 codeCode = code636 return sendInputEvent(self._fd, typeCode, codeCode, value)637 def sync(self):638 if self._fd < 1:639 raise InputDeviceError("InputDevice is not open")640 return sendInputSync(self._fd)641class InputDeviceError(Exception):642 pass643class Mouse(InputDevice):644 def __init__(self, absoluteMove=False):645 """646 Parameters:647 absoluteMove (boolean, optional)648 force move(x,y) to send absolute coordinates instead649 of standard relative movement. This helps avoiding650 mouse pointer drift in some occasions. The default651 is False.652 """653 InputDevice.__init__(self)654 self._x = 0655 self._y = 0656 self._sendAbs = absoluteMove657 def create(self, name="Virtual fMBT Mouse",658 vendor=0xf4b7, product=0x4053, version=1):659 self.startCreating(name, vendor, product, version)660 self.addEvent("EV_KEY")661 self.addEvent("EV_REL")662 if self._sendAbs:663 self.addEvent("EV_ABS")664 self.addEvent("EV_SYN")665 self.addRel("REL_X")666 self.addRel("REL_Y")667 self.addRel("REL_HWHEEL")668 self.addRel("REL_WHEEL")669 self.addKey("BTN_LEFT")670 self.addKey("BTN_RIGHT")671 self.addKey("BTN_MIDDLE")672 self.addKey("BTN_SIDE")673 self.addKey("BTN_EXTRA")674 self.addKey("BTN_FORWARD")675 self.addKey("BTN_BACK")676 self.addKey("BTN_TASK")677 if self._sendAbs:678 self.addAbs("ABS_X")679 self.addAbs("ABS_Y")680 self.finishCreating()681 return self682 def move(self, x, y):683 """684 Move mouse cursor to coordinates x, y.685 """686 if self._sendAbs:687 self.send("EV_ABS", "ABS_X", x)688 self.send("EV_ABS", "ABS_Y", y)689 else:690 deltaX = x - self._x691 deltaY = y - self._y692 self.send("EV_REL", "REL_X", deltaX)693 self.send("EV_REL", "REL_Y", deltaY)694 self.sync()695 self.setXY(x, y)696 def moveRel(self, deltaX, deltaY):697 self.send("EV_REL", "REL_X", deltaX)698 self.send("EV_REL", "REL_Y", deltaY)699 self.sync()700 self.setXY(self._x + deltaX, self._y + deltaY)701 def press(self, button):702 buttonCode = toButtonCode(button)703 self.send("EV_KEY", buttonCode, 1)704 self.sync()705 def release(self, button):706 buttonCode = toButtonCode(button)707 self.send("EV_KEY", buttonCode, 0)708 self.sync()709 def setXY(self, x, y):710 """711 Resets relative mouse position to (x, y), does not synthesize712 event. Example: disable possible mouse pointer drift:713 mouse.moveRel(-4096, -4096) # move to the top-left corner714 mouse.setXY(0, 0) # set current pointer coordinates to 0, 0715 After this, mouse.move(x, y) will synthesize relative mouse716 move event which will drive cursor to coordinates x, y.717 """718 self._x = x719 self._y = y720 def xy(self):...

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