Best JavaScript code snippet using redwood
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
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()...
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
...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.))...
manualMapper.py
Source:manualMapper.py
...30 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()
...
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
1import { createGraphQLHandler, makeMergedSchema, makeServices } from '@redwoodjs/api'2import { db } from 'src/lib/db'3import schemas from 'src/graphql/**/*.{js,ts}'4import services from 'src/services/**/*.{js,ts}'5import { setupTestDb } from '@redwoodjs/testing/api'6import { mockCurrentUser } from '@redwoodjs/testing/api'7import { mockSession } from '@redwoodjs/testing/api'8import { mockGraphQLQuery } from '@redwoodjs/testing/api'9import { mockGraphQLMutation } from '@redwoodjs/testing/api'10import { mockGraphQLHandler } from '@redwoodjs/testing/api'11import { mockGraphQLSchema } from '@redwoodjs/testing/api'12import { mockService } from '@redwoodjs/testing/api'13import { mockServices } from '@redwoodjs/testing/api'14import { mockServiceMethod } from '@redwoodjs/testing/api'15import { mockServiceMethods } from '@redwoodjs/testing/api'16import { mockHttpHandler } from '@redwoodjs/testing/api'17import { mockHttpHandlers } from '@redwoodjs/testing/api'18import { mockHttpMethod } from '@redwoodjs/testing/api'19import { mockHttpMethods } from '@redwoodjs/testing/api'20import { mockContext } from '@redwoodjs/testing/api'21import { mockContextMethod } from '@redwoodjs/testing/api'22import { mockContextMethods } from '@redwoodjs/testing/api'23import { mockDb } from '@redwoodjs/testing/api'24import { mockCurrentUser } from '@redwoodjs/testing/api'25import { mockSession } from '@redwoodjs/testing/api'26import { mockGraphQLQuery } from '@redwoodjs/testing/api'27import { mockGraphQLMutation } from '@redwoodjs/testing/api'28import { mockGraphQLHandler } from '@redwoodjs/testing/api'29import { mockGraphQLSchema } from '@redwoodjs/testing/api'30import { mockService } from '@redwoodjs/testing/api'31import { mockServices } from '@redwoodjs/testing/api'32import { mockServiceMethod } from '@redwoodjs/testing/api'33import { mockServiceMethods } from '@redwoodjs/testing/api'34import { mockHttpHandler } from '@redwoodjs/testing/api'35import { mockHttpHandlers } from '@redwoodjs/testing/api'36import { mockHttpMethod } from '@redwoodjs/testing/api'
Using AI Code Generation
1const {Mouse} = require('@redwoodjs/testing/web')2describe('MyComponent', () => {3 it('does something', () => {4 const mouse = new Mouse(cy.get('body'))5 mouse.click(100, 100)6 })7})8#### `constructor(cy: Cypress.Chainable)`9#### `click(x: number, y: number, options?: MouseOptions)`10#### `dblClick(x: number, y: number, options?: MouseOptions)`11#### `move(x: number, y: number, options?: MouseOptions)`12#### `down(x: number, y: number, options?: MouseOptions)`13#### `up(x: number, y: number, options?: MouseOptions)`14#### `wheel(x: number, y: number, options?: WheelOptions)`
Using AI Code Generation
1import { setup, cleanup, mouse } from '@redwoodjs/testing/web'2import { render } from '@redwoodjs/testing/web'3import { Loading, Empty, Failure, Success } from './PostsCell'4describe('PostsCell', () => {5 cleanup()6 setup()7 it('Loading renders successfully', () => {8 expect(() => {9 render(<Loading />)10 }).not.toThrow()11 })12 it('Empty renders successfully', () => {13 expect(() => {14 render(<Empty />)15 }).not.toThrow()16 })17 it('Failure renders successfully', () => {18 expect(() => {19 render(<Failure error={new Error('Oh no')} />)20 }).not.toThrow()21 })22 it('Success renders successfully', async () => {23 expect(() => {24 render(<Success posts={{ posts: [{ objectKey: 'objectValue' }] }} />)25 }).not.toThrow()26 })27})28import { mockGraphQLQuery } from '@redwoodjs/testing/web'29import { render, screen } from '@redwoodjs/testing/web'30import PostsCell from './PostsCell'31const mockQuery = mockGraphQLQuery('PostsQuery')32describe('PostsCell', () => {33 it('renders successfully', () => {34 expect(() => {35 render(<PostsCell />)36 }).not.toThrow()37 })38 it('calls the expected GraphQL query', async () => {39 render(<PostsCell />)40 expect(mockQuery).toHaveBeenCalledWith({41 input: {42 },43 })44 expect(mockQuery).toHaveBeenCalledTimes(1)45 })46})
Using AI Code Generation
1import { Mouse } from '@redwoodjs/testing/web'2import { click } from '@redwoodjs/testing/web'3import { render } from '@redwoodjs/testing/web'4import { screen } from '@redwoodjs/testing/web'5import { waitFor } from '@redwoodjs/testing/web'6import { waitForElementToBeRemoved } from '@redwoodjs/testing/web'7import { within } from '@redwoodjs/testing/web'8import { within } from '@redwoodjs/testing/web'9import { within } from '@redwoodjs/testing/web'10import { within } from '@redwoodjs/testing/web'11import { within } from '@redwoodjs/testing/web'12import { within } from '@redwoodjs/testing/web'13import { within } from '@redwoodjs/testing/web'14import { within } from '@redwoodjs/testing/web'15import { within } from '@redwoodjs/testing/web'16import { within } from '@redwoodjs/testing/web'
Using AI Code Generation
1import { Mouse } from '@redwoodjs/testing/web'2describe('Mouse', () => {3 it('can move the mouse', () => {4 cy.visit('/')5 cy.get('button').then((button) => {6 const mouse = new Mouse(button)7 mouse.move()8 })9 })10})11import { Keyboard } from '@redwoodjs/testing/web'12describe('Keyboard', () => {13 it('can type into an input', () => {14 cy.visit('/')15 cy.get('input').then((input) => {16 const keyboard = new Keyboard(input)17 keyboard.type('Hello, World!')18 })19 })20})21import { Interact } from '@redwoodjs/testing/web'22describe('Interact', () => {23 it('can click a button', () => {24 cy.visit('/')25 cy.get('button').then((button) => {26 const interact = new Interact(button)27 interact.click()28 })29 })30})31import { Focus } from '@redwoodjs/testing/web'32describe('Focus', () => {33 it('can focus an input', () => {34 cy.visit('/')35 cy.get('input').then((input) => {36 const focus = new Focus(input)37 focus.focus()38 })39 })40})41import { Blur } from '@redwoodjs/testing/web'42describe('Blur', () => {43 it('can blur an input', () => {44 cy.visit('/')45 cy.get('input').then((input) => {46 const blur = new Blur(input)47 blur.blur()48 })49 })50})51import { Clear } from '@redwoodjs/testing/web'52describe('Clear', () => {53 it('can clear an input', () => {54 cy.visit('/')55 cy.get('input').then((input) => {
Using AI Code Generation
1import { Mouse } from '@redwoodjs/testing/web'2import { fireEvent } from '@testing-library/dom'3import { render } from '@testing-library/react'4import { screen } from '@testing-library/dom'5import userEvent from '@testing-library/user-event'6import { within } from '@testing-library/dom'7import { waitFor } from '@testing-library/dom'8import { waitForElementToBeRemoved } from '@testing-library/dom'9import { wait } from '@testing-library/dom'10import { act } from '@testing-library/dom'11import { configure } from '@testing-library/dom'12import { createEvent } from '@testing-library/dom'13import { createEventTarget } from '@testing-library/dom'14import { getNodeText } from '@testing-library/dom'15import { prettyDOM } from '@testing-library/dom'16import { queries } from '@testing-library/dom'17import { queryHelpers } from '@testing-library/dom'18import { screen as screenDom } from '@testing-library/dom'19import { setLogger } from '@testing-library/dom'20import { wait as waitDom } from '@testing-library/dom'21import { waitFor as waitForDom } from '@testing-library/dom'22import { waitForElementToBeRemoved as waitForElementToBeRemovedDom } from '@testing-library/dom'23import {
Using AI Code Generation
1import { Mouse } from '@redwoodjs/testing/web'2describe('MyComponent', () => {3 it('does something', () => {4 cy.visit('/')5 cy.get('button').then((button) => {6 const mouse = new Mouse(button)7 mouse.click()8 mouse.dblclick()9 mouse.rightclick()10 mouse.move()11 mouse.move({ offsetX: 10, offsetY: 10 })12 mouse.move({ offsetX: 10, offsetY: 10, force: true })13 mouse.move({ offsetX: 10, offsetY: 10, force: true, animationFrame: true })14 mouse.move({15 })16 })17 })18})19import { Mouse } from '@redwoodjs/testing/web'20describe('MyComponent', () => {21 it('does something', () => {22 cy.visit('/')23 cy.get('button').then((button) => {24 const mouse = new Mouse(button)25 mouse.click()26 mouse.dblclick()27 mouse.rightclick()28 mouse.move()29 mouse.move({ offsetX: 10, offsetY: 10 })30 mouse.move({ offsetX: 10, offsetY: 10
Using AI Code Generation
1var redwood = require('redwood');2var mouse = redwood.mouse();3mouse.move(100, 100);4mouse.leftClick();5mouse.scroll(100);6mouse.move(100, 100);7mouse.rightClick();8mouse.move(100, 100);9mouse.scroll(-100);10mouse.move(100, 100);11mouse.leftClick();12mouse.move(100, 100);13mouse.rightClick();14mouse.move(100, 100);15mouse.leftClick();16mouse.move(100, 100);17mouse.rightClick();18mouse.move(100, 100);19mouse.leftClick();20mouse.move(100, 100);21mouse.rightClick();22mouse.move(100, 100);23mouse.leftClick();24mouse.move(100, 100);25mouse.rightClick();26mouse.move(100, 100);27mouse.leftClick();28mouse.move(100, 100);29mouse.rightClick();30mouse.move(100, 100);31mouse.leftClick();32mouse.move(100, 100);33mouse.rightClick();34mouse.move(100, 100);35mouse.leftClick();36mouse.move(100, 100);37mouse.rightClick();38mouse.move(100, 100);39mouse.leftClick();40mouse.move(100, 100);41mouse.rightClick();42mouse.move(100, 100);43mouse.leftClick();44mouse.move(100, 100);45mouse.rightClick();46mouse.move(100, 100);47mouse.leftClick();48mouse.move(100, 100);49mouse.rightClick();
Using AI Code Generation
1import { Mouse } from '@redwoodjs/testing/web'2it('should have a link to the home page', () => {3 const { getByText } = render(<AboutPage />)4 expect(getByText('Home')).toBeInTheDocument()5 expect(getByText('Home')).toHaveAttribute('href', '/')6})7it('should navigate to the home page when the link is clicked', async () => {8 const { getByText } = render(<AboutPage />)9 await Mouse.click(getByText('Home'))10 expect(global.location.pathname).toEqual('/')11})12import { Keyboard } from '@redwoodjs/testing/web'13it('should have a link to the home page', () => {14 const { getByText } = render(<AboutPage />)15 expect(getByText('Home')).toBeInTheDocument()16 expect(getByText('Home')).toHaveAttribute('href', '/')17})18it('should navigate to the home page when the link is clicked', async () => {19 const { getByText } = render(<AboutPage />)20 await Keyboard.press('Home')21 expect(global.location.pathname).toEqual('/')22})23import { render } from '@redwoodjs/testing/web'24it('should render successfully', () => {25 expect(() => {26 render(<AboutPage />)27 }).not.toThrow()28})29import { render } from '@redwoodjs/testing/web'30it('should render successfully', () => {31 expect(() => {32 render(<AboutPage />)33 }).not.toThrow()34})35import { render } from '@redwoodjs/testing/web'36it('should render successfully', () => {37 expect(() => {38 render(<AboutPage />)39 }).not.toThrow()40})41import { render } from '@redwoodjs/testing/web'42it('should render successfully', () => {43 expect(() => {44 render(<AboutPage />)45 }).not.toThrow()46})
Using AI Code Generation
1var mouse = require('redwood-mouse');2var mousePosition = mouse.position;3var mouseClick = mouse.click;4var keyboard = require('redwood-keyboard');5var keypress = keyboard.keypress;6var keyup = keyboard.keyup;7var keyboard = require('redwood-keyboard');8var keypress = keyboard.keypress;9var keyup = keyboard.keyup;10var keyboard = require('redwood-keyboard');11var keypress = keyboard.keypress;12var keyup = keyboard.keyup;13var keyboard = require('redwood-keyboard');14var keypress = keyboard.keypress;15var keyup = keyboard.keyup;16var keyboard = require('redwood-keyboard');17var keypress = keyboard.keypress;18var keyup = keyboard.keyup;19var keyboard = require('redwood-keyboard');20var keypress = keyboard.keypress;21var keyup = keyboard.keyup;22var keyboard = require('redwood-keyboard');23var keypress = keyboard.keypress;24var keyup = keyboard.keyup;
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!!