Best JavaScript code snippet using ghostjs
mouse_mode.py
Source:mouse_mode.py
1import os2import json3MOUSE_MODE_KEY = "semicolon"4ACTIVATE_TOGGLE = "m"5DOWN = "j"6UP = "k"7LEFT = "h"8RIGHT = "l"9FAST = "a"10SLOW = "s"11SCROLL = "d"12ARROW = "r"13LEFT_CLICK = "f"14MIDDLE_CLICK = "v"15RIGHT_CLICK = "g"16LEFT_CLICK_2 = "u"17MIDDLE_CLICK_2 = "i"18RIGHT_CLICK_2 = "o"19MOUSE_SCROLL_SPEED = 3220MOUSE_SPEED = 120021MOUSE_SLOW_MULTIPLIER = 0.2522MOUSE_FAST_MULTIPLIER = 223MOUSE_MODE = "jeffwu_mouse_mode"24# ARROW_MODE = "jeffwu_arrow_mode"25SCROLL_MODE = "jeffwu_scroll_mode"26SIMULTANEOUS_THRESHOLD_MS = 10027def var_is_set(var, value=1):28 """ Returns condition that variable is set. """29 return {30 "type": "variable_if",31 "name": var,32 "value": value33 }34def set_var(var, value=1):35 """ Returns condition that variable is set. """36 return {37 "set_variable": {38 "name": var,39 "value": value40 }41 }42def single_key(key_code, modifiers=[]):43 return {44 "key_code": key_code,45 "modifiers": {46 "mandatory": modifiers,47 "optional": [48 "any"49 ]50 }51 }52def simultaneous_keys(key_codes, after_up=None):53 res = {54 "simultaneous": [55 { "key_code": key_code } for key_code in key_codes56 ],57 "simultaneous_options": {58 "key_down_order": "strict",59 "key_up_order": "strict_inverse",60 },61 "modifiers": { "optional": [ "any" ] }62 }63 if after_up is not None:64 res["simultaneous_options"]["to_after_key_up"] = after_up65 return res66def basic_rule(items):67 return {68 "type": "basic",69 # "parameters": { "basic.simultaneous_threshold_milliseconds": SIMULTANEOUS_THRESHOLD_MS },70 **items71 }72def _scroll_combos(key):73 return [74 single_key(key, [ "left_shift" ]),75 single_key(key, [ "right_shift" ]),76 simultaneous_keys([SCROLL, key]),77 ]78def _toggle_combos():79 return [80 single_key(ACTIVATE_TOGGLE, ["left_command"]),81 single_key(ACTIVATE_TOGGLE, ["right_command"]),82 simultaneous_keys([MOUSE_MODE_KEY, ACTIVATE_TOGGLE]),83 # DONT KNOW WHY THIS DOESNT WORK84 simultaneous_keys(["escape", ACTIVATE_TOGGLE]),85 ]86# mouse_mode_rules = [87# *[88# basic_rule({89# "from": fr,90# "to": [ set_var(MOUSE_MODE, 0) ],91# "conditions": [ var_is_set(MOUSE_MODE, 1) ]92# }) for fr in [ single_key("escape"), single_key(MOUSE_MODE_KEY), single_key(ACTIVATE_TOGGLE) ]93# ],94# *[95# basic_rule({96# "from": fr,97# "to": [98# set_var(MOUSE_MODE, 1),99# ],100# "conditions": [101# var_is_set(MOUSE_MODE, 0),102# ],103# "parameters": { "basic.simultaneous_threshold_milliseconds": SIMULTANEOUS_THRESHOLD_MS },104# }) for fr in _toggle_combos()105# ],106# *[107# basic_rule({108# "from": fr,109# "to": [110# set_var(MOUSE_MODE, 0),111# ],112# "conditions": [113# var_is_set(MOUSE_MODE, 1),114# ],115# "parameters": { "basic.simultaneous_threshold_milliseconds": SIMULTANEOUS_THRESHOLD_MS },116# }) for fr in _toggle_combos()117# ],118# # NO-OP TO PREVENT WEIRD BEHAVIOR (sending d while scrolling)119# basic_rule({120# "from": single_key(UP),121# "to": [122# { "mouse_key": { "vertical_wheel": -MOUSE_SCROLL_SPEED } }123# ],124# "conditions": [125# var_is_set(MOUSE_MODE),126# var_is_set(SCROLL_MODE),127# ]128# }),129# basic_rule({130# "from": single_key(DOWN),131# "to": [132# { "mouse_key": { "vertical_wheel": MOUSE_SCROLL_SPEED } }133# ],134# "conditions": [135# var_is_set(MOUSE_MODE),136# var_is_set(SCROLL_MODE),137# ]138# }),139# basic_rule({140# "from": single_key(RIGHT),141# "to": [142# { "mouse_key": { "horizontal_wheel": MOUSE_SCROLL_SPEED } }143# ],144# "conditions": [145# var_is_set(MOUSE_MODE),146# var_is_set(SCROLL_MODE),147# ]148# }),149# basic_rule({150# "from": single_key(LEFT),151# "to": [152# { "mouse_key": { "horizontal_wheel": -MOUSE_SCROLL_SPEED } }153# ],154# "conditions": [155# var_is_set(MOUSE_MODE),156# var_is_set(SCROLL_MODE),157# ]158# }),159# *[160# basic_rule({161# "from": fr,162# "to": [163# { "mouse_key": { "vertical_wheel": -MOUSE_SCROLL_SPEED } }164# ],165# "conditions": [166# var_is_set(MOUSE_MODE),167# ]168# }) for fr in _scroll_combos(UP)169# ],170# *[171# basic_rule({172# "from": fr,173# "to": [174# { "mouse_key": { "vertical_wheel": MOUSE_SCROLL_SPEED } }175# ],176# "conditions": [177# var_is_set(MOUSE_MODE),178# ]179# }) for fr in _scroll_combos(DOWN)180# ],181# *[182# basic_rule({183# "from": fr,184# "to": [185# { "mouse_key": { "horizontal_wheel": MOUSE_SCROLL_SPEED } }186# ],187# "conditions": [188# var_is_set(MOUSE_MODE),189# ]190# }) for fr in _scroll_combos(RIGHT)191# ],192# *[193# basic_rule({194# "from": fr,195# "to": [196# { "mouse_key": { "horizontal_wheel": -MOUSE_SCROLL_SPEED } }197# ],198# "conditions": [199# var_is_set(MOUSE_MODE),200# ]201# }) for fr in _scroll_combos(LEFT)202# ],203# basic_rule({204# "from": single_key(SCROLL),205# "to": [206# set_var(MOUSE_MODE, 1),207# set_var(SCROLL_MODE, 1),208# ],209# "to_after_key_up": [ set_var(SCROLL_MODE, 0) ],210# "conditions": [211# var_is_set(MOUSE_MODE, 1),212# ]213# }),214# basic_rule({215# "from": single_key(DOWN),216# "to": [217# { "mouse_key": { "y": MOUSE_SPEED } }218# ],219# "conditions": [220# var_is_set(MOUSE_MODE),221# ]222# }),223# basic_rule({224# "from": single_key(UP),225# "to": [226# { "mouse_key": { "y": -MOUSE_SPEED } }227# ],228# "conditions": [229# var_is_set(MOUSE_MODE),230# ]231# }),232# basic_rule({233# "from": single_key(LEFT),234# "to": [235# { "mouse_key": { "x": -MOUSE_SPEED } }236# ],237# "conditions": [238# var_is_set(MOUSE_MODE),239# ]240# }),241# basic_rule({242# "from": single_key(RIGHT),243# "to": [244# { "mouse_key": { "x": MOUSE_SPEED } }245# ],246# "conditions": [247# var_is_set(MOUSE_MODE),248# ]249# }),250# *[251# basic_rule({252# "from": f,253# "to": [254# { "pointing_button": "button1" }255# ],256# "conditions": [257# var_is_set(MOUSE_MODE),258# ]259# }) for f in [single_key(LEFT_CLICK), single_key(LEFT_CLICK_2)]260# ],261# *[262# basic_rule({263# "from": f,264# "to": [265# { "pointing_button": "button3" }266# ],267# "conditions": [268# var_is_set(MOUSE_MODE),269# ]270# }) for f in [single_key(MIDDLE_CLICK), single_key(MIDDLE_CLICK_2)]271# ],272# *[273# basic_rule({274# "from": f,275# "to": [276# { "pointing_button": "button2" }277# ],278# "conditions": [279# var_is_set(MOUSE_MODE),280# ]281# }) for f in [single_key(RIGHT_CLICK), single_key(RIGHT_CLICK_2), single_key(LEFT_CLICK, ["left_shift"]), single_key(LEFT_CLICK, ["right_shift"])]282# ],283# basic_rule({284# "from": single_key(SLOW),285# "to": [286# { "mouse_key": { "speed_multiplier": MOUSE_SLOW_MULTIPLIER } }287# ],288# "conditions": [289# var_is_set(MOUSE_MODE),290# ]291# }),292# basic_rule({293# "from": single_key(FAST),294# "to": [295# { "mouse_key": { "speed_multiplier": MOUSE_FAST_MULTIPLIER } }296# ],297# "conditions": [298# var_is_set(MOUSE_MODE),299# ]300# }),301# ]302caps_lock_rules = [303 basic_rule({304 "from": single_key("caps_lock"),305 "to": [ { "key_code": "left_control" } ],306 "to_if_alone": [ { "key_code": "escape" } ],307 })308]309# NOTE: this is disabled because escape delay is too annoying310# Doing this with a simple rule instead311# right_command_rules = [312# basic_rule({313# "from": single_key("right_command"),314# "to": [ { "key_code": "right_command" } ],315# "to_if_alone": [ { "key_code": "escape" } ],316# })317# ]318shift_rules = [319 basic_rule({320 "from": { "key_code": "left_shift" },321 "to": [ { "key_code": "left_shift" } ],322 "to_if_alone": [323 {324 "key_code": "9",325 "modifiers": [ "left_shift" ]326 }327 ]328 }),329 basic_rule({330 "from": { "key_code": "right_shift" },331 "to": [ { "key_code": "right_shift" } ],332 "to_if_alone": [333 {334 "key_code": "0",335 "modifiers": [ "right_shift" ]336 }337 ]338 }),339 # rolls340 basic_rule({341 "from": {342 "key_code": "left_shift",343 "modifiers": {344 "mandatory": [ "right_shift" ]345 }346 },347 "to": [348 { "key_code": "left_shift" },349 { "key_code": "right_shift" }350 ],351 "to_if_alone": [352 {353 "key_code": "0",354 # why both?355 "modifiers": [ "right_shift", "left_shift" ]356 },357 {358 "key_code": "9",359 "modifiers": [ "right_shift", "left_shift" ]360 }361 ]362 }),363 basic_rule({364 "from": {365 "key_code": "right_shift",366 "modifiers": {367 "mandatory": [ "left_shift" ]368 }369 },370 "to": [371 { "key_code": "right_shift" },372 { "key_code": "left_shift" }373 ],374 "to_if_alone": [375 {376 "key_code": "9",377 "modifiers": [ "right_shift" ]378 },379 {380 "key_code": "0",381 "modifiers": [ "right_shift" ]382 }383 ]384 })385]386# TODO: make it so holding works387# arrow_rules = [388# basic_rule({389# "from": simultaneous_keys([ARROW, LEFT], after_up=[set_var(ARROW_MODE, 0)]),390# "to": [ { "key_code": "left_arrow" }, set_var(ARROW_MODE, 1) ],391# "parameters": { "basic.simultaneous_threshold_milliseconds": SIMULTANEOUS_THRESHOLD_MS },392# }),393# basic_rule({394# "from": simultaneous_keys([ARROW, DOWN], after_up=[set_var(ARROW_MODE, 0)]),395# "to": [ { "key_code": "down_arrow" }, set_var(ARROW_MODE, 1) ],396# "parameters": { "basic.simultaneous_threshold_milliseconds": SIMULTANEOUS_THRESHOLD_MS },397# }),398# basic_rule({399# "from": simultaneous_keys([ARROW, RIGHT], after_up=[set_var(ARROW_MODE, 0)]),400# "to": [ { "key_code": "right_arrow" }, set_var(ARROW_MODE, 1) ],401# "parameters": { "basic.simultaneous_threshold_milliseconds": SIMULTANEOUS_THRESHOLD_MS },402# }),403# basic_rule({404# "from": simultaneous_keys([ARROW, UP], after_up=[set_var(ARROW_MODE, 0)]),405# "to": [ { "key_code": "up_arrow" }, set_var(ARROW_MODE, 1) ],406# "parameters": { "basic.simultaneous_threshold_milliseconds": SIMULTANEOUS_THRESHOLD_MS },407# }),408# basic_rule({409# "from": single_key(LEFT),410# "to": [ { "key_code": "left_arrow" } ],411# "conditions": [ var_is_set(ARROW_MODE), ]412# }),413# basic_rule({414# "from": single_key(DOWN),415# "to": [ { "key_code": "down_arrow" } ],416# "conditions": [ var_is_set(ARROW_MODE), ]417# }),418# basic_rule({419# "from": single_key(RIGHT),420# "to": [ { "key_code": "right_arrow" } ],421# "conditions": [ var_is_set(ARROW_MODE), ]422# }),423# basic_rule({424# "from": single_key(UP),425# "to": [ { "key_code": "up_arrow" } ],426# "conditions": [ var_is_set(ARROW_MODE), ]427# }),428# ]429SIMULTANEOUS_THRESHOLD_MS = 100430SIMULTANEOUS_THRESHOLD_MS_RULE = { "basic.simultaneous_threshold_milliseconds": SIMULTANEOUS_THRESHOLD_MS }431MOUSE_KEYS_MOUSE_MODE = "mouse_keys_mode"432MOUSE_KEYS_SCROLL_MODE = "mouse_keys_mode_scroll"433MOUSE_KEYS_ARROW_MODE = "mouse_keys_mode_arrows"434MOUSE_KEYS_AFTER_UP = [435 set_var(MOUSE_KEYS_MOUSE_MODE, 0),436 set_var(MOUSE_KEYS_SCROLL_MODE, 0),437 set_var(MOUSE_KEYS_ARROW_MODE, 0),438]439mouse_keys_rules = [440 basic_rule({441 "from": single_key(DOWN),442 "to": [443 { "mouse_key": { "vertical_wheel": 32 } }444 ],445 "conditions": [446 var_is_set(MOUSE_KEYS_MOUSE_MODE),447 var_is_set(MOUSE_KEYS_SCROLL_MODE),448 ]449 }),450 basic_rule({451 "from": single_key(DOWN),452 "to": [453 { "key_code": "down_arrow" }454 ],455 "conditions": [456 var_is_set(MOUSE_KEYS_MOUSE_MODE),457 var_is_set(MOUSE_KEYS_ARROW_MODE),458 ]459 }),460 basic_rule({461 "from": single_key(DOWN),462 "to": [463 { "mouse_key": { "y": MOUSE_SPEED } }464 ],465 "conditions": [466 var_is_set(MOUSE_KEYS_MOUSE_MODE),467 ]468 }),469 basic_rule({470 "from": simultaneous_keys([MOUSE_MODE_KEY, DOWN], after_up=MOUSE_KEYS_AFTER_UP),471 "to": [472 set_var(MOUSE_KEYS_MOUSE_MODE, 1),473 { "mouse_key": { "y": MOUSE_SPEED } }474 ],475 "parameters": SIMULTANEOUS_THRESHOLD_MS_RULE476 }),477 basic_rule({478 "from": single_key(UP),479 "to": [480 { "mouse_key": { "vertical_wheel": -MOUSE_SCROLL_SPEED } }481 ],482 "conditions": [483 var_is_set(MOUSE_KEYS_MOUSE_MODE),484 var_is_set(MOUSE_KEYS_SCROLL_MODE),485 ]486 }),487 basic_rule({488 "from": single_key(UP),489 "to": [490 { "key_code": "up_arrow" }491 ],492 "conditions": [493 var_is_set(MOUSE_KEYS_MOUSE_MODE),494 var_is_set(MOUSE_KEYS_ARROW_MODE),495 ]496 }),497 basic_rule({498 "from": single_key(UP),499 "to": [500 { "mouse_key": { "y": -MOUSE_SPEED } }501 ],502 "conditions": [503 var_is_set(MOUSE_KEYS_MOUSE_MODE),504 ]505 }),506 basic_rule({507 "from": simultaneous_keys([MOUSE_MODE_KEY, UP], after_up=MOUSE_KEYS_AFTER_UP),508 "to": [509 set_var(MOUSE_KEYS_MOUSE_MODE, 1),510 { "mouse_key": { "y": -MOUSE_SPEED } }511 ],512 "parameters": SIMULTANEOUS_THRESHOLD_MS_RULE513 }),514 basic_rule({515 "from": single_key(LEFT),516 "to": [517 { "mouse_key": { "horizontal_wheel": MOUSE_SCROLL_SPEED } }518 ],519 "conditions": [520 var_is_set(MOUSE_KEYS_MOUSE_MODE),521 var_is_set(MOUSE_KEYS_SCROLL_MODE),522 ]523 }),524 basic_rule({525 "from": single_key(LEFT),526 "to": [527 { "key_code": "left_arrow" }528 ],529 "conditions": [530 var_is_set(MOUSE_KEYS_MOUSE_MODE),531 var_is_set(MOUSE_KEYS_ARROW_MODE),532 ]533 }),534 basic_rule({535 "from": single_key(LEFT),536 "to": [537 { "mouse_key": { "x": -MOUSE_SPEED } }538 ],539 "conditions": [540 var_is_set(MOUSE_KEYS_MOUSE_MODE),541 ]542 }),543 basic_rule({544 "from": simultaneous_keys([MOUSE_MODE_KEY, LEFT], after_up=MOUSE_KEYS_AFTER_UP),545 "to": [546 set_var(MOUSE_KEYS_MOUSE_MODE, 1),547 { "mouse_key": { "x": -MOUSE_SPEED } }548 ],549 "parameters": SIMULTANEOUS_THRESHOLD_MS_RULE550 }),551 basic_rule({552 "from": single_key(RIGHT),553 "to": [554 { "mouse_key": { "horizontal_wheel": -MOUSE_SCROLL_SPEED } }555 ],556 "conditions": [557 var_is_set(MOUSE_KEYS_MOUSE_MODE),558 var_is_set(MOUSE_KEYS_SCROLL_MODE),559 ]560 }),561 basic_rule({562 "from": single_key(RIGHT),563 "to": [564 { "key_code": "right_arrow" }565 ],566 "conditions": [567 var_is_set(MOUSE_KEYS_MOUSE_MODE),568 var_is_set(MOUSE_KEYS_ARROW_MODE),569 ]570 }),571 basic_rule({572 "from": single_key(RIGHT),573 "to": [574 { "mouse_key": { "x": MOUSE_SPEED } }575 ],576 "conditions": [577 var_is_set(MOUSE_KEYS_MOUSE_MODE),578 ]579 }),580 basic_rule({581 "from": simultaneous_keys([MOUSE_MODE_KEY, RIGHT], after_up=MOUSE_KEYS_AFTER_UP),582 "to": [583 set_var(MOUSE_KEYS_MOUSE_MODE, 1),584 { "mouse_key": { "x": MOUSE_SPEED } }585 ],586 "parameters": SIMULTANEOUS_THRESHOLD_MS_RULE587 }),588 basic_rule({589 "from": single_key(LEFT_CLICK),590 "to": [591 { "pointing_button": "button1" }592 ],593 "conditions": [594 var_is_set(MOUSE_KEYS_MOUSE_MODE),595 ]596 }),597 basic_rule({598 "from": simultaneous_keys([MOUSE_MODE_KEY, LEFT_CLICK], after_up=MOUSE_KEYS_AFTER_UP),599 "to": [600 set_var(MOUSE_KEYS_MOUSE_MODE, 1),601 { "pointing_button": "button1" }602 ],603 "parameters": SIMULTANEOUS_THRESHOLD_MS_RULE604 }),605 basic_rule({606 "from": single_key(MIDDLE_CLICK),607 "to": [608 { "pointing_button": "button3" }609 ],610 "conditions": [611 var_is_set(MOUSE_KEYS_MOUSE_MODE),612 ]613 }),614 basic_rule({615 "from": simultaneous_keys([MOUSE_MODE_KEY, MIDDLE_CLICK], after_up=MOUSE_KEYS_AFTER_UP),616 "to": [617 set_var(MOUSE_KEYS_MOUSE_MODE, 1),618 { "pointing_button": "button3" }619 ],620 "parameters": SIMULTANEOUS_THRESHOLD_MS_RULE621 }),622 basic_rule({623 "from": single_key(RIGHT_CLICK),624 "to": [625 { "pointing_button": "button2" }626 ],627 "conditions": [628 var_is_set(MOUSE_KEYS_MOUSE_MODE),629 ]630 }),631 basic_rule({632 "from": simultaneous_keys([MOUSE_MODE_KEY, RIGHT_CLICK], after_up=MOUSE_KEYS_AFTER_UP),633 "to": [634 set_var(MOUSE_KEYS_MOUSE_MODE, 1),635 { "pointing_button": "button2" }636 ],637 "parameters": SIMULTANEOUS_THRESHOLD_MS_RULE638 }),639 basic_rule({640 "from": single_key(SCROLL),641 "to": [642 set_var(MOUSE_KEYS_SCROLL_MODE, 1),643 ],644 "conditions": [645 var_is_set(MOUSE_KEYS_MOUSE_MODE),646 ],647 "to_after_key_up": [648 set_var(MOUSE_KEYS_SCROLL_MODE, 0),649 ]650 }),651 basic_rule({652 "from": simultaneous_keys([MOUSE_MODE_KEY, SCROLL], after_up=MOUSE_KEYS_AFTER_UP),653 "to": [654 set_var(MOUSE_KEYS_MOUSE_MODE, 1),655 set_var(MOUSE_KEYS_SCROLL_MODE, 1),656 ],657 "parameters": SIMULTANEOUS_THRESHOLD_MS_RULE,658 "to_after_key_up": [659 set_var(MOUSE_KEYS_SCROLL_MODE, 0),660 ]661 }),662 basic_rule({663 "from": single_key(FAST),664 "to": [665 { "mouse_key": { "speed_multiplier": MOUSE_FAST_MULTIPLIER } }666 ],667 "conditions": [668 var_is_set(MOUSE_KEYS_MOUSE_MODE),669 ]670 }),671 basic_rule({672 "from": simultaneous_keys([MOUSE_MODE_KEY, FAST], after_up=MOUSE_KEYS_AFTER_UP),673 "to": [674 set_var(MOUSE_KEYS_MOUSE_MODE, 1),675 { "mouse_key": { "speed_multiplier": MOUSE_FAST_MULTIPLIER } }676 ],677 "parameters": SIMULTANEOUS_THRESHOLD_MS_RULE678 }),679 basic_rule({680 "from": single_key(SLOW),681 "to": [682 { "mouse_key": { "speed_multiplier": MOUSE_SLOW_MULTIPLIER } }683 ],684 "conditions": [685 var_is_set(MOUSE_KEYS_MOUSE_MODE),686 ]687 }),688 basic_rule({689 "from": simultaneous_keys([MOUSE_MODE_KEY, SLOW], after_up=MOUSE_KEYS_AFTER_UP),690 "to": [691 set_var(MOUSE_KEYS_MOUSE_MODE, 1),692 { "mouse_key": { "speed_multiplier": MOUSE_SLOW_MULTIPLIER } }693 ],694 "parameters": SIMULTANEOUS_THRESHOLD_MS_RULE695 }),696 basic_rule({697 "from": single_key(ARROW),698 "to": [699 set_var(MOUSE_KEYS_ARROW_MODE, 1),700 ],701 "conditions": [702 var_is_set(MOUSE_KEYS_MOUSE_MODE),703 ],704 "to_after_key_up": [705 set_var(MOUSE_KEYS_ARROW_MODE, 0),706 ]707 }),708 basic_rule({709 "from": simultaneous_keys([MOUSE_MODE_KEY, ARROW], after_up=MOUSE_KEYS_AFTER_UP),710 "to": [711 set_var(MOUSE_KEYS_MOUSE_MODE, 1),712 set_var(MOUSE_KEYS_ARROW_MODE, 1),713 ],714 "parameters": SIMULTANEOUS_THRESHOLD_MS_RULE,715 "to_after_key_up": [716 set_var(MOUSE_KEYS_SCROLL_MODE, 0),717 ]718 })719]720with open(os.path.realpath(__file__).replace('.py', '.json'), 'w') as f:721 json.dump({722 "title": "Jeff Wu's karabiner settings",723 "rules": [724 # {725 # "description": "Mouse Mode",726 # "manipulators": mouse_mode_rules,727 # },728 {729 "description": "Mouse Keys",730 "manipulators": mouse_keys_rules,731 },732 {733 "description": "Caps Lock to Control, Escape on single press.",734 "manipulators": caps_lock_rules,735 },736 # {737 # "description": "Right Command to Escape.",738 # "manipulators": right_command_rules,739 # },740 # {741 # "description": "semicolon = arrows",742 # "manipulators": arrow_rules,743 # },744 {745 "description": "Better Shifting: Parentheses on shift keys",746 "manipulators": shift_rules,747 },748 {749 "description": "Change caps + space to backspace",750 "manipulators": [751 basic_rule({752 # use left control since we map caps to that753 "from": single_key("spacebar", [ "left_control" ]),754 "to": [ { "key_code": "delete_or_backspace" } ],755 }),756 ]757 }758 ],...
pyglet_view.py
Source:pyglet_view.py
1from builtins import object2from pyglet.window import key, mouse3from pyglet.libs.darwin.quartzkey import keymap, charmap4from pyglet.libs.darwin.cocoapy import *5NSTrackingArea = ObjCClass('NSTrackingArea')6# Event data helper functions.7def getMouseDelta(nsevent):8 dx = nsevent.deltaX()9 dy = -nsevent.deltaY()10 return int(round(dx)), int(round(dy))11def getMousePosition(self, nsevent):12 in_window = nsevent.locationInWindow()13 in_window = self.convertPoint_fromView_(in_window, None)14 x = int(in_window.x)15 y = int(in_window.y)16 # Must record mouse position for BaseWindow.draw_mouse_cursor to work.17 self._window._mouse_x = x18 self._window._mouse_y = y19 return x, y20def getModifiers(nsevent):21 modifiers = 022 modifierFlags = nsevent.modifierFlags()23 if modifierFlags & NSAlphaShiftKeyMask:24 modifiers |= key.MOD_CAPSLOCK25 if modifierFlags & NSShiftKeyMask:26 modifiers |= key.MOD_SHIFT27 if modifierFlags & NSControlKeyMask:28 modifiers |= key.MOD_CTRL29 if modifierFlags & NSAlternateKeyMask:30 modifiers |= key.MOD_ALT31 modifiers |= key.MOD_OPTION32 if modifierFlags & NSCommandKeyMask:33 modifiers |= key.MOD_COMMAND34 if modifierFlags & NSFunctionKeyMask:35 modifiers |= key.MOD_FUNCTION36 return modifiers37def getSymbol(nsevent):38 keycode = nsevent.keyCode()39 return keymap[keycode]40class PygletView_Implementation(object):41 PygletView = ObjCSubclass('NSView', 'PygletView')42 @PygletView.method(b'@'+NSRectEncoding+PyObjectEncoding)43 def initWithFrame_cocoaWindow_(self, frame, window):44 # The tracking area is used to get mouseEntered, mouseExited, and cursorUpdate 45 # events so that we can custom set the mouse cursor within the view.46 self._tracking_area = None47 self = ObjCInstance(send_super(self, 'initWithFrame:', frame, argtypes=[NSRect]))48 if not self:49 return None50 # CocoaWindow object.51 self._window = window52 self.updateTrackingAreas()53 # Create an instance of PygletTextView to handle text events. 54 # We must do this because NSOpenGLView doesn't conform to the55 # NSTextInputClient protocol by default, and the insertText: method will56 # not do the right thing with respect to translating key sequences like57 # "Option-e", "e" if the protocol isn't implemented. So the easiest58 # thing to do is to subclass NSTextView which *does* implement the59 # protocol and let it handle text input.60 PygletTextView = ObjCClass('PygletTextView')61 self._textview = PygletTextView.alloc().initWithCocoaWindow_(window)62 # Add text view to the responder chain.63 self.addSubview_(self._textview)64 return self65 @PygletView.method('v')66 def dealloc(self):67 self._window = None68 #send_message(self.objc_self, 'removeFromSuperviewWithoutNeedingDisplay')69 self._textview.release()70 self._textview = None71 self._tracking_area.release()72 self._tracking_area = None73 send_super(self, 'dealloc') 74 @PygletView.method('v')75 def updateTrackingAreas(self):76 # This method is called automatically whenever the tracking areas need to be77 # recreated, for example when window resizes.78 if self._tracking_area:79 self.removeTrackingArea_(self._tracking_area)80 self._tracking_area.release()81 self._tracking_area = None82 tracking_options = NSTrackingMouseEnteredAndExited | NSTrackingActiveInActiveApp | NSTrackingCursorUpdate83 frame = self.frame()84 self._tracking_area = NSTrackingArea.alloc().initWithRect_options_owner_userInfo_(85 frame, # rect86 tracking_options, # options87 self, # owner88 None) # userInfo89 self.addTrackingArea_(self._tracking_area)90 91 @PygletView.method('B')92 def canBecomeKeyView(self):93 return True94 @PygletView.method('B')95 def isOpaque(self):96 return True97 ## Event responders.98 # This method is called whenever the view changes size.99 @PygletView.method(b'v'+NSSizeEncoding) 100 def setFrameSize_(self, size):101 send_super(self, 'setFrameSize:', size, argtypes=[NSSize])102 # This method is called when view is first installed as the103 # contentView of window. Don't do anything on first call.104 # This also helps ensure correct window creation event ordering.105 if not self._window.context.canvas:106 return107 width, height = int(size.width), int(size.height)108 self._window.switch_to()109 self._window.context.update_geometry()110 self._window.dispatch_event("on_resize", width, height)111 self._window.dispatch_event("on_expose")112 # Can't get app.event_loop.enter_blocking() working with Cocoa, because113 # when mouse clicks on the window's resize control, Cocoa enters into a114 # mini-event loop that only responds to mouseDragged and mouseUp events.115 # This means that using NSTimer to call idle() won't work. Our kludge116 # is to override NSWindow's nextEventMatchingMask_etc method and call117 # idle() from there.118 if self.inLiveResize():119 from pyglet import app120 if app.event_loop is not None:121 app.event_loop.idle()122 @PygletView.method('v@')123 def pygletKeyDown_(self, nsevent):124 symbol = getSymbol(nsevent)125 modifiers = getModifiers(nsevent)126 self._window.dispatch_event('on_key_press', symbol, modifiers)127 @PygletView.method('v@')128 def pygletKeyUp_(self, nsevent):129 symbol = getSymbol(nsevent)130 modifiers = getModifiers(nsevent)131 self._window.dispatch_event('on_key_release', symbol, modifiers)132 @PygletView.method('v@')133 def pygletFlagsChanged_(self, nsevent):134 # Handles on_key_press and on_key_release events for modifier keys.135 # Note that capslock is handled differently than other keys; it acts136 # as a toggle, so on_key_release is only sent when it's turned off.137 # TODO: Move these constants somewhere else.138 # Undocumented left/right modifier masks found by experimentation:139 NSLeftShiftKeyMask = 1 << 1140 NSRightShiftKeyMask = 1 << 2141 NSLeftControlKeyMask = 1 << 0142 NSRightControlKeyMask = 1 << 13143 NSLeftAlternateKeyMask = 1 << 5144 NSRightAlternateKeyMask = 1 << 6145 NSLeftCommandKeyMask = 1 << 3146 NSRightCommandKeyMask = 1 << 4147 maskForKey = { key.LSHIFT : NSLeftShiftKeyMask,148 key.RSHIFT : NSRightShiftKeyMask,149 key.LCTRL : NSLeftControlKeyMask,150 key.RCTRL : NSRightControlKeyMask,151 key.LOPTION : NSLeftAlternateKeyMask,152 key.ROPTION : NSRightAlternateKeyMask,153 key.LCOMMAND : NSLeftCommandKeyMask,154 key.RCOMMAND : NSRightCommandKeyMask,155 key.CAPSLOCK : NSAlphaShiftKeyMask,156 key.FUNCTION : NSFunctionKeyMask }157 symbol = getSymbol(nsevent)158 # Ignore this event if symbol is not a modifier key. We must check this159 # because e.g., we receive a flagsChanged message when using CMD-tab to160 # switch applications, with symbol == "a" when command key is released.161 if symbol not in maskForKey: 162 return163 modifiers = getModifiers(nsevent)164 modifierFlags = nsevent.modifierFlags()165 if symbol and modifierFlags & maskForKey[symbol]:166 self._window.dispatch_event('on_key_press', symbol, modifiers)167 else:168 self._window.dispatch_event('on_key_release', symbol, modifiers)169 # Overriding this method helps prevent system beeps for unhandled events.170 @PygletView.method('B@')171 def performKeyEquivalent_(self, nsevent):172 # Let arrow keys and certain function keys pass through the responder173 # chain so that the textview can handle on_text_motion events.174 modifierFlags = nsevent.modifierFlags()175 if modifierFlags & NSNumericPadKeyMask:176 return False177 if modifierFlags & NSFunctionKeyMask:178 ch = cfstring_to_string(nsevent.charactersIgnoringModifiers())179 if ch in (NSHomeFunctionKey, NSEndFunctionKey, 180 NSPageUpFunctionKey, NSPageDownFunctionKey):181 return False182 # Send the key equivalent to the main menu to perform menu items.183 NSApp = ObjCClass('NSApplication').sharedApplication()184 NSApp.mainMenu().performKeyEquivalent_(nsevent)185 # Indicate that we've handled the event so system won't beep.186 return True187 @PygletView.method('v@')188 def mouseMoved_(self, nsevent):189 if self._window._mouse_ignore_motion:190 self._window._mouse_ignore_motion = False191 return192 # Don't send on_mouse_motion events if we're not inside the content rectangle.193 if not self._window._mouse_in_window:194 return195 x, y = getMousePosition(self, nsevent)196 dx, dy = getMouseDelta(nsevent)197 self._window.dispatch_event('on_mouse_motion', x, y, dx, dy)198 199 @PygletView.method('v@')200 def scrollWheel_(self, nsevent):201 x, y = getMousePosition(self, nsevent)202 scroll_x, scroll_y = getMouseDelta(nsevent)203 self._window.dispatch_event('on_mouse_scroll', x, y, scroll_x, scroll_y)204 205 @PygletView.method('v@')206 def mouseDown_(self, nsevent):207 x, y = getMousePosition(self, nsevent)208 buttons = mouse.LEFT209 modifiers = getModifiers(nsevent)210 self._window.dispatch_event('on_mouse_press', x, y, buttons, modifiers)211 212 @PygletView.method('v@')213 def mouseDragged_(self, nsevent):214 x, y = getMousePosition(self, nsevent)215 dx, dy = getMouseDelta(nsevent)216 buttons = mouse.LEFT217 modifiers = getModifiers(nsevent)218 self._window.dispatch_event('on_mouse_drag', x, y, dx, dy, buttons, modifiers)219 @PygletView.method('v@')220 def mouseUp_(self, nsevent):221 x, y = getMousePosition(self, nsevent)222 buttons = mouse.LEFT223 modifiers = getModifiers(nsevent)224 self._window.dispatch_event('on_mouse_release', x, y, buttons, modifiers)225 226 @PygletView.method('v@')227 def rightMouseDown_(self, nsevent):228 x, y = getMousePosition(self, nsevent)229 buttons = mouse.RIGHT230 modifiers = getModifiers(nsevent)231 self._window.dispatch_event('on_mouse_press', x, y, buttons, modifiers)232 233 @PygletView.method('v@')234 def rightMouseDragged_(self, nsevent):235 x, y = getMousePosition(self, nsevent)236 dx, dy = getMouseDelta(nsevent)237 buttons = mouse.RIGHT238 modifiers = getModifiers(nsevent)239 self._window.dispatch_event('on_mouse_drag', x, y, dx, dy, buttons, modifiers)240 241 @PygletView.method('v@')242 def rightMouseUp_(self, nsevent):243 x, y = getMousePosition(self, nsevent)244 buttons = mouse.RIGHT245 modifiers = getModifiers(nsevent)246 self._window.dispatch_event('on_mouse_release', x, y, buttons, modifiers)247 248 @PygletView.method('v@')249 def otherMouseDown_(self, nsevent):250 x, y = getMousePosition(self, nsevent)251 buttons = mouse.MIDDLE252 modifiers = getModifiers(nsevent)253 self._window.dispatch_event('on_mouse_press', x, y, buttons, modifiers)254 255 @PygletView.method('v@')256 def otherMouseDragged_(self, nsevent):257 x, y = getMousePosition(self, nsevent)258 dx, dy = getMouseDelta(nsevent)259 buttons = mouse.MIDDLE260 modifiers = getModifiers(nsevent)261 self._window.dispatch_event('on_mouse_drag', x, y, dx, dy, buttons, modifiers)262 263 @PygletView.method('v@')264 def otherMouseUp_(self, nsevent):265 x, y = getMousePosition(self, nsevent)266 buttons = mouse.MIDDLE267 modifiers = getModifiers(nsevent)268 self._window.dispatch_event('on_mouse_release', x, y, buttons, modifiers)269 @PygletView.method('v@')270 def mouseEntered_(self, nsevent):271 x, y = getMousePosition(self, nsevent)272 self._window._mouse_in_window = True273 # Don't call self._window.set_mouse_platform_visible() from here.274 # Better to do it from cursorUpdate:275 self._window.dispatch_event('on_mouse_enter', x, y)276 @PygletView.method('v@')277 def mouseExited_(self, nsevent):278 x, y = getMousePosition(self, nsevent)279 self._window._mouse_in_window = False280 if not self._window._is_mouse_exclusive:281 self._window.set_mouse_platform_visible()282 self._window.dispatch_event('on_mouse_leave', x, y)283 @PygletView.method('v@')284 def cursorUpdate_(self, nsevent):285 # Called when mouse cursor enters view. Unlike mouseEntered:,286 # this method will be called if the view appears underneath a287 # motionless mouse cursor, as can happen during window creation,288 # or when switching into fullscreen mode. 289 # BUG: If the mouse enters the window via the resize control at the290 # the bottom right corner, the resize control will set the cursor291 # to the default arrow and screw up our cursor tracking.292 self._window._mouse_in_window = True293 if not self._window._is_mouse_exclusive:294 self._window.set_mouse_platform_visible()...
_mouse_tests.py
Source:_mouse_tests.py
1# -*- coding: utf-8 -*-2import unittest3import time4from ._mouse_event import MoveEvent, ButtonEvent, WheelEvent, LEFT, RIGHT, MIDDLE, X, X2, UP, DOWN, DOUBLE5from keyboard import mouse6class FakeOsMouse(object):7 def __init__(self):8 self.append = None9 self.position = (0, 0)10 self.queue = None11 self.init = lambda: None12 def listen(self, queue):13 self.listening = True14 self.queue = queue15 def press(self, button):16 self.append((DOWN, button))17 def release(self, button):18 self.append((UP, button))19 def get_position(self):20 return self.position21 def move_to(self, x, y):22 self.append(('move', (x, y)))23 self.position = (x, y)24 def wheel(self, delta):25 self.append(('wheel', delta))26 def move_relative(self, x, y):27 self.position = (self.position[0] + x, self.position[1] + y)28class TestMouse(unittest.TestCase):29 @staticmethod30 def setUpClass():31 mouse._os_mouse= FakeOsMouse()32 mouse._listener.start_if_necessary()33 assert mouse._os_mouse.listening34 def setUp(self):35 self.events = []36 mouse._pressed_events.clear()37 mouse._os_mouse.append = self.events.append38 def tearDown(self):39 mouse.unhook_all()40 # Make sure there's no spill over between tests.41 self.wait_for_events_queue()42 def wait_for_events_queue(self):43 mouse._listener.queue.join()44 def flush_events(self):45 self.wait_for_events_queue()46 events = list(self.events)47 # Ugly, but requried to work in Python2. Python3 has list.clear48 del self.events[:]49 return events50 def press(self, button=LEFT):51 mouse._os_mouse.queue.put(ButtonEvent(DOWN, button, time.time()))52 self.wait_for_events_queue()53 def release(self, button=LEFT):54 mouse._os_mouse.queue.put(ButtonEvent(UP, button, time.time()))55 self.wait_for_events_queue()56 def double_click(self, button=LEFT):57 mouse._os_mouse.queue.put(ButtonEvent(DOUBLE, button, time.time()))58 self.wait_for_events_queue()59 def click(self, button=LEFT):60 self.press(button)61 self.release(button)62 def wheel(self, delta=1):63 mouse._os_mouse.queue.put(WheelEvent(delta, time.time()))64 self.wait_for_events_queue()65 def move(self, x=0, y=0):66 mouse._os_mouse.queue.put(MoveEvent(x, y, time.time()))67 self.wait_for_events_queue()68 def test_hook(self):69 events = []70 self.press()71 mouse.hook(events.append)72 self.press()73 mouse.unhook(events.append)74 self.press()75 self.assertEqual(len(events), 1)76 def test_is_pressed(self):77 self.assertFalse(mouse.is_pressed())78 self.press()79 self.assertTrue(mouse.is_pressed())80 self.release()81 self.press(X2)82 self.assertFalse(mouse.is_pressed())83 self.assertTrue(mouse.is_pressed(X2))84 self.press(X2)85 self.assertTrue(mouse.is_pressed(X2))86 self.release(X2)87 self.release(X2)88 self.assertFalse(mouse.is_pressed(X2))89 def test_buttons(self):90 mouse.press()91 self.assertEqual(self.flush_events(), [(DOWN, LEFT)])92 mouse.release()93 self.assertEqual(self.flush_events(), [(UP, LEFT)])94 mouse.click()95 self.assertEqual(self.flush_events(), [(DOWN, LEFT), (UP, LEFT)])96 mouse.double_click()97 self.assertEqual(self.flush_events(), [(DOWN, LEFT), (UP, LEFT), (DOWN, LEFT), (UP, LEFT)])98 mouse.right_click()99 self.assertEqual(self.flush_events(), [(DOWN, RIGHT), (UP, RIGHT)])100 mouse.click(RIGHT)101 self.assertEqual(self.flush_events(), [(DOWN, RIGHT), (UP, RIGHT)])102 mouse.press(X2)103 self.assertEqual(self.flush_events(), [(DOWN, X2)])104 def test_position(self):105 self.assertEqual(mouse.get_position(), mouse._os_mouse.get_position())106 def test_move(self):107 mouse.move(0, 0)108 self.assertEqual(mouse._os_mouse.get_position(), (0, 0))109 mouse.move(100, 500)110 self.assertEqual(mouse._os_mouse.get_position(), (100, 500))111 mouse.move(1, 2, False)112 self.assertEqual(mouse._os_mouse.get_position(), (101, 502))113 mouse.move(0, 0)114 mouse.move(100, 499, True, duration=0.01)115 self.assertEqual(mouse._os_mouse.get_position(), (100, 499))116 mouse.move(100, 1, False, duration=0.01)117 self.assertEqual(mouse._os_mouse.get_position(), (200, 500))118 mouse.move(0, 0, False, duration=0.01)119 self.assertEqual(mouse._os_mouse.get_position(), (200, 500))120 def triggers(self, fn, events, **kwargs):121 self.triggered = False122 def callback():123 self.triggered = True124 handler = fn(callback, **kwargs)125 for event_type, arg in events:126 if event_type == DOWN:127 self.press(arg)128 elif event_type == UP:129 self.release(arg)130 elif event_type == DOUBLE:131 self.double_click(arg)132 elif event_type == 'WHEEL':133 self.wheel()134 mouse._listener.remove_handler(handler)135 return self.triggered136 def test_on_button(self):137 self.assertTrue(self.triggers(mouse.on_button, [(DOWN, LEFT)]))138 self.assertTrue(self.triggers(mouse.on_button, [(DOWN, RIGHT)]))139 self.assertTrue(self.triggers(mouse.on_button, [(DOWN, X)]))140 self.assertFalse(self.triggers(mouse.on_button, [('WHEEL', '')]))141 self.assertFalse(self.triggers(mouse.on_button, [(DOWN, X)], buttons=MIDDLE))142 self.assertTrue(self.triggers(mouse.on_button, [(DOWN, MIDDLE)], buttons=MIDDLE))143 self.assertTrue(self.triggers(mouse.on_button, [(DOWN, MIDDLE)], buttons=MIDDLE))144 self.assertFalse(self.triggers(mouse.on_button, [(DOWN, MIDDLE)], buttons=MIDDLE, types=UP))145 self.assertTrue(self.triggers(mouse.on_button, [(UP, MIDDLE)], buttons=MIDDLE, types=UP))146 self.assertTrue(self.triggers(mouse.on_button, [(UP, MIDDLE)], buttons=[MIDDLE, LEFT], types=[UP, DOWN]))147 self.assertTrue(self.triggers(mouse.on_button, [(DOWN, LEFT)], buttons=[MIDDLE, LEFT], types=[UP, DOWN]))148 self.assertFalse(self.triggers(mouse.on_button, [(UP, X)], buttons=[MIDDLE, LEFT], types=[UP, DOWN]))149 def test_ons(self):150 self.assertTrue(self.triggers(mouse.on_click, [(UP, LEFT)]))151 self.assertFalse(self.triggers(mouse.on_click, [(UP, RIGHT)]))152 self.assertFalse(self.triggers(mouse.on_click, [(DOWN, LEFT)]))153 self.assertFalse(self.triggers(mouse.on_click, [(DOWN, RIGHT)]))154 self.assertTrue(self.triggers(mouse.on_double_click, [(DOUBLE, LEFT)]))155 self.assertFalse(self.triggers(mouse.on_double_click, [(DOUBLE, RIGHT)]))156 self.assertFalse(self.triggers(mouse.on_double_click, [(DOWN, RIGHT)]))157 self.assertTrue(self.triggers(mouse.on_right_click, [(UP, RIGHT)]))158 self.assertTrue(self.triggers(mouse.on_middle_click, [(UP, MIDDLE)]))159 def test_wait(self):160 # If this fails it blocks. Unfortunately, but I see no other way of testing.161 from threading import Thread, Lock162 lock = Lock()163 lock.acquire()164 def t():165 mouse.wait()166 lock.release()167 Thread(target=t).start()168 self.press()169 lock.acquire()170 def test_record_play(self):171 from threading import Thread, Lock172 lock = Lock()173 lock.acquire()174 def t():175 self.recorded = mouse.record(RIGHT)176 lock.release()177 Thread(target=t).start()178 self.click()179 self.wheel(5)180 self.move(100, 50)181 self.press(RIGHT)182 lock.acquire()183 self.assertEqual(len(self.recorded), 5)184 self.assertEqual(self.recorded[0]._replace(time=None), ButtonEvent(DOWN, LEFT, None))185 self.assertEqual(self.recorded[1]._replace(time=None), ButtonEvent(UP, LEFT, None))186 self.assertEqual(self.recorded[2]._replace(time=None), WheelEvent(5, None))187 self.assertEqual(self.recorded[3]._replace(time=None), MoveEvent(100, 50, None))188 self.assertEqual(self.recorded[4]._replace(time=None), ButtonEvent(DOWN, RIGHT, None))189 mouse.play(self.recorded, speed_factor=0)190 events = self.flush_events()191 self.assertEqual(len(events), 5)192 self.assertEqual(events[0], (DOWN, LEFT))193 self.assertEqual(events[1], (UP, LEFT))194 self.assertEqual(events[2], ('wheel', 5))195 self.assertEqual(events[3], ('move', (100, 50)))196 self.assertEqual(events[4], (DOWN, RIGHT))197 mouse.play(self.recorded)198 events = self.flush_events()199 self.assertEqual(len(events), 5)200 self.assertEqual(events[0], (DOWN, LEFT))201 self.assertEqual(events[1], (UP, LEFT))202 self.assertEqual(events[2], ('wheel', 5))203 self.assertEqual(events[3], ('move', (100, 50)))204 self.assertEqual(events[4], (DOWN, RIGHT))205 mouse.play(self.recorded, include_clicks=False)206 events = self.flush_events()207 self.assertEqual(len(events), 2)208 self.assertEqual(events[0], ('wheel', 5))209 self.assertEqual(events[1], ('move', (100, 50)))210 mouse.play(self.recorded, include_moves=False)211 events = self.flush_events()212 self.assertEqual(len(events), 4)213 self.assertEqual(events[0], (DOWN, LEFT))214 self.assertEqual(events[1], (UP, LEFT))215 self.assertEqual(events[2], ('wheel', 5))216 self.assertEqual(events[3], (DOWN, RIGHT))217 mouse.play(self.recorded, include_wheel=False)218 events = self.flush_events()219 self.assertEqual(len(events), 4)220 self.assertEqual(events[0], (DOWN, LEFT))221 self.assertEqual(events[1], (UP, LEFT))222 self.assertEqual(events[2], ('move', (100, 50)))223 self.assertEqual(events[3], (DOWN, RIGHT))224if __name__ == '__main__':...
mouse_test.py
Source:mouse_test.py
1import unittest2class MouseModuleTest(unittest.TestCase):3 def todo_test_get_cursor(self):4 # __doc__ (as of 2008-08-02) for pygame.mouse.get_cursor:5 # pygame.mouse.get_cursor(): return (size, hotspot, xormasks, andmasks)6 # get the image for the system mouse cursor7 #8 # Get the information about the mouse system cursor. The return value9 # is the same data as the arguments passed into10 # pygame.mouse.set_cursor().11 #12 self.fail()13 def todo_test_get_focused(self):14 # __doc__ (as of 2008-08-02) for pygame.mouse.get_focused:15 # pygame.mouse.get_focused(): return bool16 # check if the display is receiving mouse input17 #18 # Returns true when pygame is receiving mouse input events (or, in19 # windowing terminology, is "active" or has the "focus").20 #21 # This method is most useful when working in a window. By contrast, in22 # full-screen mode, this method always returns true.23 #24 # Note: under MS Windows, the window that has the mouse focus also has25 # the keyboard focus. But under X-Windows, one window can receive26 # mouse events and another receive keyboard events.27 # pygame.mouse.get_focused() indicates whether the pygame window28 # receives mouse events.29 #30 self.fail()31 def todo_test_get_pos(self):32 # __doc__ (as of 2008-08-02) for pygame.mouse.get_pos:33 # pygame.mouse.get_pos(): return (x, y)34 # get the mouse cursor position35 #36 # Returns the X and Y position of the mouse cursor. The position is37 # relative the the top-left corner of the display. The cursor position38 # can be located outside of the display window, but is always39 # constrained to the screen.40 #41 self.fail()42 def todo_test_get_pressed(self):43 # __doc__ (as of 2008-08-02) for pygame.mouse.get_pressed:44 # pygame.moouse.get_pressed(): return (button1, button2, button3)45 # get the state of the mouse buttons46 #47 # Returns a sequence of booleans representing the state of all the48 # mouse buttons. A true value means the mouse is currently being49 # pressed at the time of the call.50 #51 # Note, to get all of the mouse events it is better to use either52 # pygame.event.wait() or pygame.event.get() and check all of those events53 # to see if they are MOUSEBUTTONDOWN, MOUSEBUTTONUP, or MOUSEMOTION.54 # Note, that on X11 some XServers use middle button emulation. When55 # you click both buttons 1 and 3 at the same time a 2 button event can56 # be emitted.57 #58 # Note, remember to call pygame.event.get() before this function.59 # Otherwise it will not work.60 #61 self.fail()62 def todo_test_get_rel(self):63 # __doc__ (as of 2008-08-02) for pygame.mouse.get_rel:64 # pygame.mouse.get_rel(): return (x, y)65 # get the amount of mouse movement66 #67 # Returns the amount of movement in X and Y since the previous call to68 # this function. The relative movement of the mouse cursor is69 # constrained to the edges of the screen, but see the virtual input70 # mouse mode for a way around this. Virtual input mode is described71 # at the top of the page.72 #73 self.fail()74 def todo_test_set_cursor(self):75 # __doc__ (as of 2008-08-02) for pygame.mouse.set_cursor:76 # pygame.mouse.set_cursor(size, hotspot, xormasks, andmasks): return None77 # set the image for the system mouse cursor78 #79 # When the mouse cursor is visible, it will be displayed as a black80 # and white bitmap using the given bitmask arrays. The size is a81 # sequence containing the cursor width and height. Hotspot is a82 # sequence containing the cursor hotspot position. xormasks is a83 # sequence of bytes containing the cursor xor data masks. Lastly is84 # andmasks, a sequence of bytes containting the cursor bitmask data.85 #86 # Width must be a multiple of 8, and the mask arrays must be the87 # correct size for the given width and height. Otherwise an exception88 # is raised.89 #90 # See the pygame.cursor module for help creating default and custom91 # masks for the system cursor.92 #93 self.fail()94 def todo_test_set_pos(self):95 # __doc__ (as of 2008-08-02) for pygame.mouse.set_pos:96 # pygame.mouse.set_pos([x, y]): return None97 # set the mouse cursor position98 #99 # Set the current mouse position to arguments given. If the mouse100 # cursor is visible it will jump to the new coordinates. Moving the101 # mouse will generate a new pygaqme.MOUSEMOTION event.102 #103 self.fail()104 def todo_test_set_visible(self):105 # __doc__ (as of 2008-08-02) for pygame.mouse.set_visible:106 # pygame.mouse.set_visible(bool): return bool107 # hide or show the mouse cursor108 #109 # If the bool argument is true, the mouse cursor will be visible. This110 # will return the previous visible state of the cursor.111 #112 self.fail()113################################################################################114if __name__ == '__main__':...
tst3.py
Source:tst3.py
1#!/usr/bin/env python2'''3Demonstrate different mouse modes4'''5# from panda3d.core import loadPrcFileData6#7# loadPrcFileData("", "notify-level-x11display debug")8# loadPrcFileData("", "notify-level-windisplay debug")9#10# loadPrcFileData("", "load-display p3tinydisplay")11from panda3d.core import WindowProperties, TextNode12from direct.task.TaskManagerGlobal import taskMgr13from direct.gui.OnscreenText import OnscreenText14from direct.task import Task15from direct.showbase.ShowBase import ShowBase16import sys17class App(ShowBase):18 def __init__(self):19 ShowBase.__init__(self)20 self.base = self21 self.setup()22 def genLabelText(self, text, i):23 text = OnscreenText(text = text, pos = (-1.3, .5-.05*i), fg=(0,1,0,1),24 align = TextNode.ALeft, scale = .05)25 return text26 def setup(self):27 # Disable the camera trackball controls.28 self.disableMouse()29 # control mapping of mouse movement to box movement30 self.mouseMagnitude = 131 self.rotateX, self.rotateY = 0, 032 self.genLabelText("[0] Absolute mode, [1] Relative mode, [2] Confined mode", 0)33 self.base.accept('0', lambda: self.setMouseMode(WindowProperties.M_absolute))34 self.base.accept('1', lambda: self.setMouseMode(WindowProperties.M_relative))35 self.base.accept('2', lambda: self.setMouseMode(WindowProperties.M_confined))36 self.genLabelText("[C] Manually re-center mouse on each tick", 1)37 self.base.accept('C', lambda: self.toggleRecenter())38 self.base.accept('c', lambda: self.toggleRecenter())39 self.genLabelText("[S] Show mouse", 2)40 self.base.accept('S', lambda: self.toggleMouse())41 self.base.accept('s', lambda: self.toggleMouse())42 self.base.accept('escape', sys.exit, [0])43 self.mouseText = self.genLabelText("", 5)44 self.deltaText = self.genLabelText("", 6)45 self.positionText = self.genLabelText("", 8)46 self.lastMouseX, self.lastMouseY = None, None47 self.hideMouse = False48 self.setMouseMode(WindowProperties.M_absolute)49 self.manualRecenterMouse = True50 # make a box to move with the mouse51 self.model = self.loader.loadModel("box")52 self.model.reparentTo(self.render)53 self.cam.setPos(0, -5, 0)54 self.cam.lookAt(0, 0, 0)55 self.mouseTask = taskMgr.add(self.mouseTask, "Mouse Task")56 def setMouseMode(self, mode):57 print("Changing mode to %s" % mode)58 self.mouseMode = mode59 wp = WindowProperties()60 wp.setMouseMode(mode)61 self.base.win.requestProperties(wp)62 # these changes may require a tick to apply63 self.base.taskMgr.doMethodLater(0, self.resolveMouse, "Resolve mouse setting")64 def resolveMouse(self, t):65 wp = self.base.win.getProperties()66 actualMode = wp.getMouseMode()67 if self.mouseMode != actualMode:68 print("ACTUAL MOUSE MODE: %s" % actualMode)69 self.mouseMode = actualMode70 self.rotateX, self.rotateY = -.5, -.571 self.lastMouseX, self.lastMouseY = None, None72 self.recenterMouse()73 def recenterMouse(self):74 self.base.win.movePointer(0,75 int(self.base.win.getProperties().getXSize() / 2),76 int(self.base.win.getProperties().getYSize() / 2))77 def toggleRecenter(self):78 print("Toggling re-center behavior")79 self.manualRecenterMouse = not self.manualRecenterMouse80 def toggleMouse(self):81 print("Toggling mouse visibility")82 self.hideMouse = not self.hideMouse83 wp = WindowProperties()84 wp.setCursorHidden(self.hideMouse)85 self.base.win.requestProperties(wp)86 def mouseTask(self, task):87 mw = self.base.mouseWatcherNode88 hasMouse = mw.hasMouse()89 if hasMouse:90 # get the window manager's idea of the mouse position91 x, y = mw.getMouseX(), mw.getMouseY()92 if self.lastMouseX is not None:93 # get the delta94 if self.manualRecenterMouse:95 # when recentering, the position IS the delta96 # since the center is reported as 0, 097 dx, dy = x, y98 else:99 dx, dy = x - self.lastMouseX, y - self.lastMouseY100 else:101 # no data to compare with yet102 dx, dy = 0, 0103 self.lastMouseX, self.lastMouseY = x, y104 else:105 x, y, dx, dy = 0, 0, 0, 0106 if self.manualRecenterMouse:107 # move mouse back to center108 self.recenterMouse()109 self.lastMouseX, self.lastMouseY = 0, 0110 # scale position and delta to pixels for user111 w, h = self.win.getSize()112 self.mouseText.setText("Mode: {0}, Recenter: {1} | Mouse: {2}, {3} | hasMouse: {4}".format(113 self.mouseMode, self.manualRecenterMouse,114 int(x*w), int(y*h),115 hasMouse))116 self.deltaText.setText("Delta: {0}, {1}".format(117 int(dx*w), int(dy*h)))118 # rotate box by delta119 self.rotateX += dx * 10 * self.mouseMagnitude120 self.rotateY += dy * 10 * self.mouseMagnitude121 self.positionText.setText("Model rotation: {0}, {1}".format(122 int(self.rotateX*1000)/1000., int(self.rotateY*1000)/1000.))123 self.model.setH(self.rotateX)124 self.model.setP(self.rotateY)125 return Task.cont126app = App()...
manualMapper.py
Source:manualMapper.py
1# -*- coding: utf-8 -*-2"""3Created on Wed Mar 09 12:36:46 201645@author: corbettb6"""78from psychopy import visual, event9import numpy as np10from VisStimControl import VisStimControl111213class manualMapper(VisStimControl):14 15 def __init__(self):16 VisStimControl.__init__(self) 17 self.boxSize = 50 #degrees?18 self.boxColors = (-1, 1)19 self.boxDuration = 10 #number of frames to show each box20 self.trialBoxPosition = [0, 0] # x and y 21 self.boxPositionHistory = []22 self.boxColorHistory = []23 self.toggleColor = False24 self.togglePeriod = 30 #frames between toggles25 self._save = False26 27 def run(self):28 29 #setup monitor30 self.prepareRun()31 self._win.units = 'pix'32 33 event.clearEvents()34 mouse = event.Mouse(win=self._win)35 mouse.setVisible(0)3637 stim = visual.Rect(self._win)38 stim.height = self.boxSize39 stim.width = self.boxSize40 stim.pos = (self.trialBoxPosition[0], self.trialBoxPosition[1])41 stim.fillColor = self.boxColors[0]42 stim.lineColor = self.boxColors[0]43 lastMouseWheelButton = 044 numFrames = 0 45 mousePos = np.array([0,0])46 while True:4748# mousePos = mouse.getPos()49 mousePos += mouse.getRel()50 stim.pos = (mousePos[0], mousePos[1])51 if self.toggleColor and (numFrames % self.togglePeriod == 0):52 stim.fillColor = -stim.fillColor53 stim.lineColor = -stim.lineColor54 55 stim.draw()56 57 self.visStimFlip()58 keys = event.getKeys(['space', 'escape', 'period', 'comma', 'up', 'down', 'p'])59 mouseWheel = mouse.getWheelRel()60 mouseButtons = mouse.getPressed()61 stepSize = 1062 if max(mouseButtons[0], mouseButtons[2])==0:63 stim.height += stepSize*mouseWheel[1]64 stim.width += stepSize*mouseWheel[1]65 else:66 if [mouseButtons[0], mouseButtons[2]] == [1, 0]:67 stim.height += stepSize*mouseWheel[1]68 if [mouseButtons[0], mouseButtons[2]] == [0, 1]:69 stim.width += stepSize*mouseWheel[1]70 if [mouseButtons[0], mouseButtons[2]] == [1, 1]:71 stim.height = (stim.height + stim.width)/272 stim.width = (stim.height + stim.width)/273 if mouseButtons[1] - lastMouseWheelButton == 1:74 stim.fillColor = -stim.fillColor75 stim.lineColor = -stim.lineColor76 77 if len(keys) > 0:78 if 'space' in keys:79 self.toggleColor = False if self.toggleColor else True80 if 'comma' in keys:81 self.togglePeriod -= 582 self.togglePeriod = 1 if self.togglePeriod <= 0 else self.togglePeriod83 if 'period' in keys:84 self.togglePeriod += 585 if 'up' in keys:86 stim.ori += 587 if 'down' in keys:88 stim.ori -= 589 if 'escape' in keys:90 break91 if 'p' in keys:92 print [pos/self.pixelsPerDeg for pos in stim.pos]93 lastMouseWheelButton = mouseButtons[1]94 numFrames += 195 96 event.clearEvents()97 self.completeRun()98 99# def set_params(self):100# self.trialBoxColor = np.random.choice(self.boxColors)101# self.trialBoxPosition[0] = np.random.randint(-self.monPix[0]/2, self.monPix[0]/2)102# self.trialBoxPosition[1] = np.random.randint(-self.monPix[1]/2, self.monPix[1]/2) 103 104 105if __name__ == "__main__":
...
controler.py
Source:controler.py
1import pygame2from game import Game3class Controler:4 def __init__(self, game):5 self.__game = game6 self.__button_flag = True7 self.__button_flag2 = True8 self.__mouse_pos = (0, 0)9 self.__mouse_down = False10 self.__mouse_moving = False11 self.__mouse_draging = False12 def Event(self):13 keys = pygame.key.get_pressed()14 mods = pygame.key.get_mods()15 mouse_keys = pygame.mouse.get_pressed()16 # event handling17 for event in pygame.event.get():18 if event.type == pygame.QUIT:19 pygame.quit()20 quit()21 if event.type == pygame.MOUSEBUTTONUP:22 if mouse_keys[0]:23 if (self.__mouse_down and not self.__mouse_moving):24 self.__game.Click(self.__mouse_pos[0], self.__mouse_pos[1])25 self.__mouse_down = False26 self.__mouse_moving = False27 self.__mouse_draging = False28 if event.type == pygame.MOUSEBUTTONDOWN:29 self.__mouse_pos = pygame.mouse.get_pos()30 self.__mouse_down = True31 self.__mouse_moving = False32 if event.button == 4:33 self.__game.Zoom(-10)34 if event.button == 5:35 self.__game.Zoom(10)36 if event.type == pygame.MOUSEMOTION:37 self.__mouse_moving = True38 39 if(self.__mouse_down):40 if (mods & pygame.KMOD_LCTRL):41 if (mouse_keys[0]):42 pos = pygame.mouse.get_pos()43 self.__game.Click(pos[0], pos[1], True, True)44 self.__mouse_draging = False45 elif (mouse_keys[2]):46 pos = pygame.mouse.get_pos()47 self.__game.Click(pos[0], pos[1], True, False)48 self.__mouse_draging = False49 else:50 if (self.__mouse_draging):51 self.__game.Move(pygame.mouse.get_rel())52 else:53 pygame.mouse.get_rel()54 self.__mouse_draging = True55 # keys presing handling56 if keys[pygame.K_ESCAPE]:57 pygame.quit()58 quit()59 elif keys[pygame.K_LEFT] or keys[pygame.K_a]:60 self.__game.Move((10, 0))61 elif keys[pygame.K_RIGHT] or keys[pygame.K_d]:62 self.__game.Move((-10, 0))63 elif keys[pygame.K_UP] or keys[pygame.K_w]:64 self.__game.Move((0, 10))65 elif keys[pygame.K_DOWN] or keys[pygame.K_s]:66 self.__game.Move((0, -10))67 elif keys[pygame.K_q]:68 self.__game.Zoom(1)69 elif keys[pygame.K_e]:70 self.__game.Zoom(-1)71 elif keys[pygame.K_SPACE]:72 self.__game.Tick()73 elif keys[pygame.K_n] and self.__button_flag:74 self.__button_flag = False75 elif keys[pygame.K_n] == False and self.__button_flag == False:76 self.__game.Tick()77 self.__button_flag = True78 # delay...
mouse_events.py
Source:mouse_events.py
1"""2Mouse events.3How it works4------------5The renderer has a 2 dimensional grid of mouse event handlers.6(`prompt_toolkit.layout.MouseHandlers`.) When the layout is rendered, the7`Window` class will make sure that this grid will also be filled with8callbacks. For vt100 terminals, mouse events are received through stdin, just9like any other key press. There is a handler among the key bindings that10catches these events and forwards them to such a mouse event handler. It passes11through the `Window` class where the coordinates are translated from absolute12coordinates to coordinates relative to the user control, and there13`UIControl.mouse_handler` is called.14"""15from __future__ import unicode_literals16__all__ = (17 'MouseEventType',18 'MouseEvent'19)20class MouseEventType:21 MOUSE_UP = 'MOUSE_UP'22 MOUSE_DOWN = 'MOUSE_DOWN'23 SCROLL_UP = 'SCROLL_UP'24 SCROLL_DOWN = 'SCROLL_DOWN'25MouseEventTypes = MouseEventType # Deprecated: plural for backwards compatibility.26class MouseEvent(object):27 """28 Mouse event, sent to `UIControl.mouse_handler`.29 :param position: `Point` instance.30 :param event_type: `MouseEventType`.31 """32 def __init__(self, position, event_type):33 self.position = position34 self.event_type = event_type35 def __repr__(self):...
Using AI Code Generation
1var ghost = require('ghostjs');2ghost.create().then(function(ghost) {3 ghost.mouseMove(100, 100).then(function() {4 ghost.mouseClick().then(function() {5 ghost.exit();6 });7 });8 });9});10#### Ghost.create([options])11#### Ghost.open(url)12#### Ghost.close()13#### Ghost.exit()14#### Ghost.mouseMove(x, y)15#### Ghost.mouseClick()16#### Ghost.mouseDown()17#### Ghost.mouseUp()18#### Ghost.mouseDoubleClick()19#### Ghost.mouseMoveToElement(element)20#### Ghost.mouseClickOnElement(element)21#### Ghost.mouseDoubleClickOnElement(element)22#### Ghost.mouseDownOnElement(element)23#### Ghost.mouseUpOnElement(element)24#### Ghost.mouseMoveToElementWithOffset(element, x, y)
Using AI Code Generation
1var ghost = require("ghostjs");2ghost.set("viewportSize", {width: 1024, height: 768});3ghost.mouse("click", {x: 100, y: 100});4ghost.mouse("move", {x: 200, y: 200});5ghost.mouse("click", {x: 200, y: 200});6ghost.mouse("move", {x: 100, y: 100});7ghost.mouse("click", {x: 100, y: 100});8ghost.run(function(err, nightmare) {9 if (err) return console.log(err);10 console.log("Done!");11});12var ghost = require("ghostjs");13ghost.set("viewportSize", {width: 1024, height: 768});14ghost.touch("tap", {x: 100, y: 100});15ghost.touch("move", {x: 200, y: 200});16ghost.touch("tap", {x: 200, y: 200});17ghost.touch("move", {x: 100, y: 100});18ghost.touch("tap", {x: 100, y: 100});19ghost.run(function(err, nightmare) {20 if (err) return console.log(err);21 console.log("Done!");22});23var ghost = require("ghostjs");24ghost.set("viewportSize", {width: 1024, height: 768});25ghost.wait(10000);26ghost.run(function(err, nightmare) {27 if (err) return console.log(err);28 console.log("Done!");29});30var ghost = require("ghostjs");31ghost.set("viewportSize", {width: 1024, height: 768});32ghost.waitFor(function() {33 return document.querySelector("input[name='q']");34});35ghost.run(function(err, nightmare) {36 if (err) return console.log(err);37 console.log("Done!");38});
Using AI Code Generation
1var ghost = require("ghostjs");2ghost.create(function(err, g) {3 g.mouse.move(100, 100);4 g.mouse.move(100, 200);5 g.mouse.move(100, 300);6 g.mouse.move(100, 400);7 g.mouse.move(100, 500);8 g.mouse.move(100, 600);9 g.mouse.move(100, 700);10 g.mouse.move(100, 800);11 g.mouse.move(100, 900);12 g.mouse.move(100, 1000);13 g.mouse.move(100, 1100);14 g.mouse.move(100, 1200);15 g.mouse.move(100, 1300);16 g.mouse.move(100, 1400);17 g.mouse.move(100, 1500);18 g.mouse.move(100, 1600);19 g.mouse.move(100, 1700);20 g.mouse.move(100, 1800);21 g.mouse.move(100, 1900);22 g.mouse.move(100, 2000);23 g.mouse.move(100, 2100);24 g.mouse.move(100, 2200);25 g.mouse.move(100, 2300);26 g.mouse.move(100, 2400);27 g.mouse.move(100, 2500);28 g.mouse.move(100, 2600);29 g.mouse.move(100, 2700);30 g.mouse.move(100, 2800);31 g.mouse.move(100, 2900);32 g.mouse.move(100, 3000);33 g.mouse.move(100, 3100);34 g.mouse.move(100, 3200);35 g.mouse.move(100, 3300);36 g.mouse.move(100, 3400);37 g.mouse.move(100, 3500);38 g.mouse.move(100, 3600);39 g.mouse.move(100, 3700);40 g.mouse.move(100, 3800);41 g.mouse.move(100, 3900);42 g.mouse.move(100, 4000);43 g.mouse.move(100, 4100);44 g.mouse.move(100, 4200);45 g.mouse.move(100, 4300);46 g.mouse.move(100, 4400);47 g.mouse.move(100,
Using AI Code Generation
1var ghost = require('ghostjs');2ghost.create(function(err, g) {3 g.mouse.move(100, 100);4 g.mouse.click();5 });6});7var ghost = require('ghostjs');8ghost.create(function(err, g) {9 g.keyboard.type('Hello World');10 });11});12### ghost.create([options], callback)13### ghost.close(callback)14### ghost.open(url, callback)15### ghost.screenshot(filename, callback)16### ghost.html(callback)17### ghost.title(callback)18* `callback(err
Using AI Code Generation
1var ghost = require('ghostjs'),2 assert = require('assert');3ghost.create().then(function(ghost){4 ghost.mouse.click(100, 100).then(function(){5 ghost.wait(1000).then(function(){6 ghost.screenshot().then(function(image){7 image.write('screenshot.png');8 ghost.exit();9 });10 });11 });12 });13});14var ghost = require('ghostjs'),15 assert = require('assert');16ghost.create().then(function(ghost){17 ghost.mouse.click(100, 100).then(function(){18 ghost.wait(1000).then(function(){19 ghost.screenshot().then(function(image){20 image.write('screenshot.png');21 ghost.exit();22 });23 });24 });25 });26});27var ghost = require('ghostjs'),28 assert = require('assert');29ghost.create().then(function(ghost){30 ghost.mouse.click(100, 100).then(function(){31 ghost.wait(1000).then(function(){32 ghost.screenshot().then(function(image){33 image.write('screenshot.png');34 ghost.exit();35 });36 });37 });38 });39});40var ghost = require('ghostjs'),41 assert = require('assert');42ghost.create().then(function(ghost){43 ghost.mouse.click(100, 100).then(function(){44 ghost.wait(1000).then(function(){45 ghost.screenshot().then(function(image){46 image.write('screenshot.png');47 ghost.exit();48 });49 });50 });51 });52});53var ghost = require('ghostjs'),54 assert = require('assert');55ghost.create().then(function(ghost){56 ghost.mouse.click(100, 100).then(function(){57 ghost.wait(1000).then(function(){58 ghost.screenshot().then(function(image){59 image.write('s
Using AI Code Generation
1var ghost = require('ghostjs');2 if (err) return console.log("Error: " + err);3 ghost.mouse.move(100, 100, function(err) {4 if (err) return console.log("Error: " + err);5 ghost.mouse.click(function(err) {6 if (err) return console.log("Error: " + err);7 ghost.exit();8 });9 });10});11var ghost = require('ghostjs');12 if (err) return console.log("Error: " + err);13 ghost.keyboard.type("ghostjs", function(err) {14 if (err) return console.log("Error: " + err);15 ghost.exit();16 });17});18### open(url, callback)19 if (err) return console.log("Error: " + err);20 ghost.exit();21});22### exit()23ghost.exit();24### scroll(x, y, callback)25ghost.scroll(0, 100, function(err) {26 if (err) return console.log("Error: " + err);27 ghost.exit();28});29#### move(x, y, callback)30ghost.mouse.move(100, 100, function(err) {31 if (err) return console.log("Error: " + err);32 ghost.exit();33});34#### click(callback)35ghost.mouse.click(function(err) {36 if (err) return console.log("Error: " + err);37 ghost.exit();38});39#### clickRight(callback)40ghost.mouse.clickRight(function(err) {41 if (err) return console.log("Error: " + err);42 ghost.exit();43});44#### doubleClick(callback)45ghost.mouse.doubleClick(function(err) {46 if (err) return console.log("Error: "
Using AI Code Generation
1var ghost = require('ghostjs');2 ghost.mouse.move(100, 100);3 ghost.mouse.click(100, 100);4});5var ghost = require('ghostjs');6 ghost.mouse.move(100, 100);7});8var ghost = require('ghostjs');9 ghost.mouse.click(100, 100);10});11var ghost = require('ghostjs');12 ghost.mouse.rightClick(100, 100);13});14var ghost = require('ghostjs');15 ghost.mouse.doubleClick(100, 100);16});17var ghost = require('ghostjs');18 ghost.mouse.down(100, 100);19});
Using AI Code Generation
1var mouse = require('ghostjs').mouse;2mouse.click(100,100);3#### mouse.click(x,y)4#### mouse.down(x,y)5#### mouse.up(x,y)6#### mouse.move(x,y)7#### mouse.doubleClick(x,y)8#### mouse.rightClick(x,y)9#### mouse.drag(fromX,fromY,toX,toY)10#### mouse.scrollUp(x,y)11#### mouse.scrollDown(x,y)12#### mouse.scrollLeft(x,y)13#### mouse.scrollRight(x,y)14#### mouse.dragAndDrop(fromElement, toElement)15- `fromElement` (object) - The element to drag16- `toElement` (object) - The element to drop the mouse on17#### mouse.dragAndDropByOffset(fromElement, x, y)18- `fromElement` (object) - The element to drag19- `x` (number) - The x coordinate to drop the mouse on20- `y` (number) - The y coordinate to drop the mouse on
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!