How to use MouseInput method in fMBT

Best Python code snippet using fMBT_python

_win32.py

Source:_win32.py Github

copy

Full Screen

1# coding=utf-82# pynput3# Copyright (C) 2015-2020 Moses Palmér4#5# This program is free software: you can redistribute it and/or modify it under6# the terms of the GNU Lesser General Public License as published by the Free7# Software Foundation, either version 3 of the License, or (at your option) any8# later version.9#10# This program is distributed in the hope that it will be useful, but WITHOUT11# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS12# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more13# details.14#15# You should have received a copy of the GNU Lesser General Public License16# along with this program. If not, see <http://www.gnu.org/licenses/>.17"""18The mouse implementation for *Windows*.19"""20# pylint: disable=C011121# The documentation is extracted from the base classes22# pylint: disable=R090323# We implement stubs24import ctypes25import enum26from ctypes import (27 windll,28 wintypes)29from pynput._util import NotifierMixin30from pynput._util.win32 import (31 INPUT,32 INPUT_union,33 ListenerMixin,34 MOUSEINPUT,35 SendInput,36 SystemHook)37from . import _base38#: A constant used as a factor when constructing mouse scroll data.39WHEEL_DELTA = 12040class Button(enum.Enum):41 """The various buttons.42 """43 unknown = None44 left = (MOUSEINPUT.LEFTUP, MOUSEINPUT.LEFTDOWN, 0)45 middle = (MOUSEINPUT.MIDDLEUP, MOUSEINPUT.MIDDLEDOWN, 0)46 right = (MOUSEINPUT.RIGHTUP, MOUSEINPUT.RIGHTDOWN, 0)47 x1 = (MOUSEINPUT.XUP, MOUSEINPUT.XDOWN, MOUSEINPUT.XBUTTON1)48 x2 = (MOUSEINPUT.XUP, MOUSEINPUT.XDOWN, MOUSEINPUT.XBUTTON2)49class Controller(NotifierMixin, _base.Controller):50 __GetCursorPos = windll.user32.GetCursorPos51 __SetCursorPos = windll.user32.SetCursorPos52 def __init__(self, *args, **kwargs):53 super(Controller, self).__init__(*args, **kwargs)54 def _position_get(self):55 point = wintypes.POINT()56 if self.__GetCursorPos(ctypes.byref(point)):57 return (point.x, point.y)58 else:59 return None60 def _position_set(self, pos):61 pos = int(pos[0]), int(pos[1])62 self.__SetCursorPos(*pos)63 self._emit('on_move', *pos)64 def _scroll(self, dx, dy):65 if dy:66 SendInput(67 1,68 ctypes.byref(INPUT(69 type=INPUT.MOUSE,70 value=INPUT_union(71 mi=MOUSEINPUT(72 dwFlags=MOUSEINPUT.WHEEL,73 mouseData=int(dy * WHEEL_DELTA))))),74 ctypes.sizeof(INPUT))75 if dx:76 SendInput(77 1,78 ctypes.byref(INPUT(79 type=INPUT.MOUSE,80 value=INPUT_union(81 mi=MOUSEINPUT(82 dwFlags=MOUSEINPUT.HWHEEL,83 mouseData=int(dx * WHEEL_DELTA))))),84 ctypes.sizeof(INPUT))85 if dx or dy:86 px, py = self._position_get()87 self._emit('on_scroll', px, py, dx, dy)88 def _press(self, button):89 SendInput(90 1,91 ctypes.byref(INPUT(92 type=INPUT.MOUSE,93 value=INPUT_union(94 mi=MOUSEINPUT(95 dwFlags=button.value[1],96 mouseData=button.value[2])))),97 ctypes.sizeof(INPUT))98 def _release(self, button):99 SendInput(100 1,101 ctypes.byref(INPUT(102 type=INPUT.MOUSE,103 value=INPUT_union(104 mi=MOUSEINPUT(105 dwFlags=button.value[0],106 mouseData=button.value[2])))),107 ctypes.sizeof(INPUT))108@Controller._receiver109class Listener(ListenerMixin, _base.Listener):110 #: The Windows hook ID for low level mouse events, ``WH_MOUSE_LL``111 _EVENTS = 14112 WM_LBUTTONDOWN = 0x0201113 WM_LBUTTONUP = 0x0202114 WM_MBUTTONDOWN = 0x0207115 WM_MBUTTONUP = 0x0208116 WM_MOUSEMOVE = 0x0200117 WM_MOUSEWHEEL = 0x020A118 WM_MOUSEHWHEEL = 0x020E119 WM_RBUTTONDOWN = 0x0204120 WM_RBUTTONUP = 0x0205121 WM_XBUTTONDOWN = 0x20B122 WM_XBUTTONUP = 0x20C123 MK_XBUTTON1 = 0x0020124 MK_XBUTTON2 = 0x0040125 XBUTTON1 = 1126 XBUTTON2 = 2127 #: A mapping from messages to button events128 CLICK_BUTTONS = {129 WM_LBUTTONDOWN: (Button.left, True),130 WM_LBUTTONUP: (Button.left, False),131 WM_MBUTTONDOWN: (Button.middle, True),132 WM_MBUTTONUP: (Button.middle, False),133 WM_RBUTTONDOWN: (Button.right, True),134 WM_RBUTTONUP: (Button.right, False)}135 #: A mapping from message to X button events.136 X_BUTTONS = {137 WM_XBUTTONDOWN: {138 XBUTTON1: (Button.x1, True),139 XBUTTON2: (Button.x2, True)},140 WM_XBUTTONUP: {141 XBUTTON1: (Button.x1, False),142 XBUTTON2: (Button.x2, False)}}143 #: A mapping from messages to scroll vectors144 SCROLL_BUTTONS = {145 WM_MOUSEWHEEL: (0, 1),146 WM_MOUSEHWHEEL: (1, 0)}147 _HANDLED_EXCEPTIONS = (148 SystemHook.SuppressException,)149 class _MSLLHOOKSTRUCT(ctypes.Structure):150 """Contains information about a mouse event passed to a ``WH_MOUSE_LL``151 hook procedure, ``MouseProc``.152 """153 _fields_ = [154 ('pt', wintypes.POINT),155 ('mouseData', wintypes.DWORD),156 ('flags', wintypes.DWORD),157 ('time', wintypes.DWORD),158 ('dwExtraInfo', ctypes.c_void_p)]159 #: A pointer to a :class:`_MSLLHOOKSTRUCT`160 _LPMSLLHOOKSTRUCT = ctypes.POINTER(_MSLLHOOKSTRUCT)161 def __init__(self, *args, **kwargs):162 super(Listener, self).__init__(*args, **kwargs)163 self._event_filter = self._options.get(164 'event_filter',165 lambda msg, data: True)166 def _handle(self, code, msg, lpdata):167 if code != SystemHook.HC_ACTION:168 return169 data = ctypes.cast(lpdata, self._LPMSLLHOOKSTRUCT).contents170 # Suppress further propagation of the event if it is filtered171 if self._event_filter(msg, data) is False:172 return173 if msg == self.WM_MOUSEMOVE:174 self.on_move(data.pt.x, data.pt.y)175 elif msg in self.CLICK_BUTTONS:176 button, pressed = self.CLICK_BUTTONS[msg]177 self.on_click(data.pt.x, data.pt.y, button, pressed)178 elif msg in self.X_BUTTONS:179 button, pressed = self.X_BUTTONS[msg][data.mouseData >> 16]180 self.on_click(data.pt.x, data.pt.y, button, pressed)181 elif msg in self.SCROLL_BUTTONS:182 mx, my = self.SCROLL_BUTTONS[msg]183 dd = wintypes.SHORT(data.mouseData >> 16).value // WHEEL_DELTA...

Full Screen

Full Screen

MainMenu.py

Source:MainMenu.py Github

copy

Full Screen

1from os import environ2environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1'3import pygame4import MainGame5from math import ceil67from pygame.locals import (8 MOUSEBUTTONDOWN,9 KEYDOWN,10 K_ESCAPE,11 QUIT,12)131415class MainMenu:16 def __init__(self):17 pygame.init()18 pygame.display.set_caption("PyChess")19 pygame.display.set_icon(pygame.image.load("sprites/icon.png"))20 screeninfo = pygame.display.Info()21 self.resfactor = int(screeninfo.current_h * 0.94)22 self.screen = pygame.Surface([1024, 1024])23 self.display = pygame.display.set_mode([self.resfactor, self.resfactor])24 self.running = True25 self.mouseinput = None26 self.clock = pygame.time.Clock()27 self.bg = pygame.image.load("sprites/mainmenu.png")28 self.selectionWhite = pygame.image.load("sprites/white_menusetting.png")29 self.selectionBlack = pygame.image.load("sprites/black_menusetting.png")30 self.selectionsprite = pygame.image.load("sprites/menuselection.png").convert_alpha()31 self.buttonRange = [self.returnEdge(0.21875),32 self.returnEdge(0.434570),33 self.returnEdge(0.546875),34 self.returnEdge(0.659179),35 self.returnEdge(0.771484),36 self.returnEdge(0.39746),37 self.returnEdge(0.64746),38 self.returnEdge(0.68554),39 self.returnEdge(0.93554),40 self.returnEdge(0.83691),41 self.returnEdge(0.93066)]42 self.startingside = "White"4344 def runMenu(self):45 while self.running:46 self.renderDisplay()47 self.checkEvents()48 self.clock.tick(60)4950 def renderDisplay(self):51 self.screen.blit(self.bg, (0, 0))52 if self.startingside == "White":53 self.screen.blit(self.selectionsprite, (self.buttonRange[5], self.buttonRange[9]))54 else:55 self.screen.blit(self.selectionsprite, (self.buttonRange[7], self.buttonRange[9]))56 self.screen.blit(self.selectionWhite, (self.buttonRange[5], self.buttonRange[9]))57 self.screen.blit(self.selectionBlack, (self.buttonRange[7], self.buttonRange[9]))58 frame = pygame.transform.scale(self.screen, (self.resfactor, self.resfactor))59 self.display.blit(frame, frame.get_rect())60 pygame.display.flip()6162 def checkEvents(self):63 for event in pygame.event.get():64 if event.type == MOUSEBUTTONDOWN:65 self.mouseinput = event.pos66 if self.mouseinput[0] in range(self.buttonRange[0], self.resfactor - self.buttonRange[0]) and \67 self.mouseinput[1] in range(self.buttonRange[1], self.buttonRange[2]):68 gameexec = MainGame.Game(self.startingside)69 gameexec.game.aiEnabled = False70 gameexec.runGame()71 elif self.mouseinput[0] in range(self.buttonRange[0], self.resfactor - self.buttonRange[0]) and \72 self.mouseinput[1] in range(self.buttonRange[2], self.buttonRange[3]):73 exe = MainGame.Game(self.startingside)74 exe.game.aiEnabled = True75 exe.runGame()76 elif self.mouseinput[0] in range(self.buttonRange[0], self.resfactor - self.buttonRange[0]) and \77 self.mouseinput[1] in range(self.buttonRange[3], self.buttonRange[4]):78 self.running = False79 elif self.mouseinput[0] in range(self.buttonRange[5], self.buttonRange[6]) and \80 self.mouseinput[1] in range(self.buttonRange[9], self.buttonRange[10]):81 self.startingside = "White"82 elif self.mouseinput[0] in range(self.buttonRange[7], self.buttonRange[8]) and \83 self.mouseinput[1] in range(self.buttonRange[9], self.buttonRange[10]):84 self.startingside = "Black"85 elif event.type == KEYDOWN:86 if event.key == K_ESCAPE:87 self.running = False88 elif event.type == QUIT:89 self.running = False9091 def returnEdge(self, x): ...

Full Screen

Full Screen

Automation Testing Tutorials

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

LambdaTest Learning Hubs:

YouTube

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

Run fMBT automation tests on LambdaTest cloud grid

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

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful