Skip to content

Commit

Permalink
style: KeyCodes should be singular
Browse files Browse the repository at this point in the history
  • Loading branch information
winstxnhdw committed Aug 12, 2024
1 parent 8e0fc92 commit d5565fc
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 59 deletions.
47 changes: 21 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ pip install git+https://github.com/winstxnhdw/KeyWin
`KeyWin` provides a set of pre-mapped key codes for common keys. These key codes are defined [here](keywin/keyboard/codes/__init__.py).

```python
from keywin import KeyCodes, keyboard
from keywin import KeyCode, keyboard

# Enter
keyboard.press(KeyCodes.VK_RETURN)
keyboard.press(KeyCode.VK_RETURN)

# Win + D
keyboard.press(KeyCodes.VK_LWIN, KeyCodes.VK_D)
keyboard.press(KeyCode.VK_LWIN, KeyCode.VK_D)
```

#### Manual Key Codes
Expand Down Expand Up @@ -127,19 +127,19 @@ mouse.scroll_horizontal(-10)
Rarely, you may want to use the low-level API for low-latency inputs. `create_event()` is a helper function that returns a cacheable `MOUSEINPUT` list, which can be passed to the low-level wrapper function `send_events()`.

```python
from keywin import mouse, MouseCodes
from keywin import mouse, MouseCode


class Bot:

def __init__(self):

self.left_click_event = mouse.create_event(
MouseCodes.MOUSE_LEFT_CLICK, 100, 100
MouseCode.MOUSE_LEFT_CLICK, 100, 100
)

self.right_click_event = mouse.create_event(
MouseCodes.MOUSE_RIGHT_CLICK, 100, 100
MouseCode.MOUSE_RIGHT_CLICK, 100, 100
)


Expand All @@ -154,17 +154,17 @@ class Bot:
`KeyWin` also provides a typesafe `SendInput` wrapper that can be used to send any input event. This function can be more performant for long sequences of inputs. The following example demonstrates how to move the mouse and bring up the task manager.

```python
from keywin import KeyCodes, MouseCodes
from keywin import KeyCode, MouseCode
from keywin.generic import send_input

send_input(
{'key': KeyCodes.VK_LCONTROL, 'release': False},
{'key': KeyCodes.VK_LSHIFT, 'release': False},
{'key': KeyCodes.VK_ESCAPE, 'release': False},
{'key': KeyCodes.VK_LCONTROL, 'release': 2},
{'key': KeyCodes.VK_LSHIFT, 'release': 2},
{'key': KeyCodes.VK_ESCAPE, 'release': 2},
{'x': 0, 'y': 0, 'data': 0, 'flags': MouseCodes.MOUSE_MOVE_ABSOLUTE},
{'key': KeyCode.VK_LCONTROL, 'release': False},
{'key': KeyCode.VK_LSHIFT, 'release': False},
{'key': KeyCode.VK_ESCAPE, 'release': False},
{'key': KeyCode.VK_LCONTROL, 'release': 2},
{'key': KeyCode.VK_LSHIFT, 'release': 2},
{'key': KeyCode.VK_ESCAPE, 'release': 2},
{'x': 0, 'y': 0, 'data': 0, 'flags': MouseCode.MOUSE_MOVE_ABSOLUTE},
)
```

Expand All @@ -181,12 +181,12 @@ send_input(
```python
import cProfile as profile

from keywin import keyboard, KeyCodes
from keywin import keyboard, KeyCode


def keywin():

keyboard.press(KeyCodes.VK_SPACE)
keyboard.press(KeyCode.VK_SPACE)


if __name__ == '__main__':
Expand Down Expand Up @@ -322,23 +322,18 @@ ncalls tottime percall cumtime percall filename:lineno(function)
```python
import cProfile as profile

from keywin import mouse, MouseCodes
from keywin import mouse, MouseCode


def keywin():

move_absolute_flag = MouseCodes.MOUSEEVENTF_MOVE | MouseCodes.MOUSEEVENTF_ABSOLUTE
left_click_flag = MouseCodes.MOUSEEVENTF_LEFTDOWN | MouseCodes.MOUSEEVENTF_LEFTUP
right_click_flag = MouseCodes.MOUSEEVENTF_RIGHTDOWN | MouseCodes.MOUSEEVENTF_RIGHTUP
desired_position = (100, 100)

# Left + Right click at (100, 100)
mouse.send_events([
*desired_position, 0, move_absolute_flag | left_click_flag
],
[
*desired_position, 0, move_absolute_flag | right_click_flag
])
mouse.send_events(
[*desired_position, 0, MouseCode.MOUSE_MOVE_ABSOLUTE | MouseCode.MOUSE_LEFT_CLICK],
[*desired_position, 0, MouseCode.MOUSE_MOVE_ABSOLUTE | MouseCode.MOUSE_RIGHT_CLICK]
)


if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions keywin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from keywin.keyboard.codes import KeyCodes as KeyCodes
from keywin.mouse.codes import MouseCodes as MouseCodes
from keywin.keyboard.key_codes import KeyCode as KeyCode
from keywin.mouse.mouse_code import MouseCode as MouseCode
2 changes: 1 addition & 1 deletion keywin/keyboard/codes.py → keywin/keyboard/key_codes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class KeyCodes:
class KeyCode:
"""
Summary
-------
Expand Down
40 changes: 20 additions & 20 deletions keywin/mouse/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from keywin.mouse.codes import MouseCodes
from keywin.mouse.mouse_code import MouseCode
from keywin.send_input import send_mouse_events, send_mouse_flag


Expand Down Expand Up @@ -51,7 +51,7 @@ def left_click() -> bool:
--------
success (bool) : the success of the event
"""
return send_mouse_flag(MouseCodes.MOUSE_LEFT_CLICK)
return send_mouse_flag(MouseCode.MOUSE_LEFT_CLICK)


def right_click() -> bool:
Expand All @@ -64,7 +64,7 @@ def right_click() -> bool:
--------
success (bool) : the success of the event
"""
return send_mouse_flag(MouseCodes.MOUSE_RIGHT_CLICK)
return send_mouse_flag(MouseCode.MOUSE_RIGHT_CLICK)


def middle_click() -> bool:
Expand All @@ -77,7 +77,7 @@ def middle_click() -> bool:
--------
success (bool) : the success of the event
"""
return send_mouse_flag(MouseCodes.MOUSE_MIDDLE_CLICK)
return send_mouse_flag(MouseCode.MOUSE_MIDDLE_CLICK)


def xbutton1_click() -> bool:
Expand All @@ -90,7 +90,7 @@ def xbutton1_click() -> bool:
--------
success (bool) : the success of the event
"""
return send_events((0, 0, MouseCodes.XBUTTON1, MouseCodes.MOUSE_XBUTTON1_CLICK))
return send_events((0, 0, MouseCode.XBUTTON1, MouseCode.MOUSE_XBUTTON1_CLICK))


def xbutton2_click() -> bool:
Expand All @@ -103,7 +103,7 @@ def xbutton2_click() -> bool:
--------
success (bool) : the success of the event
"""
return send_events((0, 0, MouseCodes.XBUTTON2, MouseCodes.MOUSE_XBUTTON2_CLICK))
return send_events((0, 0, MouseCode.XBUTTON2, MouseCode.MOUSE_XBUTTON2_CLICK))


def left_press() -> bool:
Expand All @@ -116,7 +116,7 @@ def left_press() -> bool:
--------
success (bool) : the success of the event
"""
return send_mouse_flag(MouseCodes.MOUSEEVENTF_LEFTDOWN)
return send_mouse_flag(MouseCode.MOUSEEVENTF_LEFTDOWN)


def left_release() -> bool:
Expand All @@ -129,7 +129,7 @@ def left_release() -> bool:
--------
success (bool) : the success of the event
"""
return send_mouse_flag(MouseCodes.MOUSEEVENTF_LEFTUP)
return send_mouse_flag(MouseCode.MOUSEEVENTF_LEFTUP)


def right_press() -> bool:
Expand All @@ -142,7 +142,7 @@ def right_press() -> bool:
--------
success (bool) : the success of the event
"""
return send_mouse_flag(MouseCodes.MOUSEEVENTF_RIGHTDOWN)
return send_mouse_flag(MouseCode.MOUSEEVENTF_RIGHTDOWN)


def right_release() -> bool:
Expand All @@ -155,7 +155,7 @@ def right_release() -> bool:
--------
success (bool) : the success of the event
"""
return send_mouse_flag(MouseCodes.MOUSEEVENTF_RIGHTUP)
return send_mouse_flag(MouseCode.MOUSEEVENTF_RIGHTUP)


def middle_press() -> bool:
Expand All @@ -168,7 +168,7 @@ def middle_press() -> bool:
--------
success (bool) : the success of the event
"""
return send_mouse_flag(MouseCodes.MOUSEEVENTF_MIDDLEDOWN)
return send_mouse_flag(MouseCode.MOUSEEVENTF_MIDDLEDOWN)


def middle_release() -> bool:
Expand All @@ -181,7 +181,7 @@ def middle_release() -> bool:
--------
success (bool) : the success of the event
"""
return send_mouse_flag(MouseCodes.MOUSEEVENTF_MIDDLEUP)
return send_mouse_flag(MouseCode.MOUSEEVENTF_MIDDLEUP)


def xbutton1_press() -> bool:
Expand All @@ -194,7 +194,7 @@ def xbutton1_press() -> bool:
--------
success (bool) : the success of the event
"""
return send_events((0, 0, MouseCodes.XBUTTON1, MouseCodes.MOUSEEVENTF_XDOWN))
return send_events((0, 0, MouseCode.XBUTTON1, MouseCode.MOUSEEVENTF_XDOWN))


def xbutton1_release() -> bool:
Expand All @@ -207,7 +207,7 @@ def xbutton1_release() -> bool:
--------
success (bool) : the success of the event
"""
return send_events((0, 0, MouseCodes.XBUTTON1, MouseCodes.MOUSEEVENTF_XUP))
return send_events((0, 0, MouseCode.XBUTTON1, MouseCode.MOUSEEVENTF_XUP))


def xbutton2_press() -> bool:
Expand All @@ -220,7 +220,7 @@ def xbutton2_press() -> bool:
--------
success (bool) : the success of the event
"""
return send_events((0, 0, MouseCodes.XBUTTON2, MouseCodes.MOUSEEVENTF_XDOWN))
return send_events((0, 0, MouseCode.XBUTTON2, MouseCode.MOUSEEVENTF_XDOWN))


def xbutton2_release() -> bool:
Expand All @@ -233,7 +233,7 @@ def xbutton2_release() -> bool:
--------
success (bool) : the success of the event
"""
return send_events((0, 0, MouseCodes.XBUTTON2, MouseCodes.MOUSEEVENTF_XUP))
return send_events((0, 0, MouseCode.XBUTTON2, MouseCode.MOUSEEVENTF_XUP))


def move_relative(x: int, y: int) -> bool:
Expand All @@ -251,7 +251,7 @@ def move_relative(x: int, y: int) -> bool:
--------
success (bool) : the success of the event
"""
return send_events((x, y, 0, MouseCodes.MOUSEEVENTF_MOVE))
return send_events((x, y, 0, MouseCode.MOUSEEVENTF_MOVE))


def move(x: int, y: int) -> bool:
Expand All @@ -269,7 +269,7 @@ def move(x: int, y: int) -> bool:
--------
success (bool) : the success of the event
"""
return send_events((x, y, 0, MouseCodes.MOUSE_MOVE_ABSOLUTE))
return send_events((x, y, 0, MouseCode.MOUSE_MOVE_ABSOLUTE))


def scroll(scroll_delta: int) -> bool:
Expand All @@ -286,7 +286,7 @@ def scroll(scroll_delta: int) -> bool:
--------
success (bool) : the success of the event
"""
return send_events((0, 0, scroll_delta, MouseCodes.MOUSEEVENTF_WHEEL))
return send_events((0, 0, scroll_delta, MouseCode.MOUSEEVENTF_WHEEL))


def scroll_horizontal(scroll_delta: int) -> bool:
Expand All @@ -303,4 +303,4 @@ def scroll_horizontal(scroll_delta: int) -> bool:
--------
success (bool) : the success of the event
"""
return send_events((0, 0, scroll_delta, MouseCodes.MOUSEEVENTF_HWHEEL))
return send_events((0, 0, scroll_delta, MouseCode.MOUSEEVENTF_HWHEEL))
2 changes: 1 addition & 1 deletion keywin/mouse/codes.py → keywin/mouse/mouse_code.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class MouseCodes:
class MouseCode:
"""
Summary
-------
Expand Down
18 changes: 9 additions & 9 deletions tests/test_keywin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from keywin import KeyCodes, MouseCodes
from keywin import KeyCode, MouseCode
from keywin.generic import send_input
from keywin.keyboard import press
from keywin.mouse import (
Expand Down Expand Up @@ -31,13 +31,13 @@ def test_send_input():
test the `send_input` function
"""
assert send_input(
{'key': KeyCodes.VK_LCONTROL, 'release': False},
{'key': KeyCodes.VK_LSHIFT, 'release': False},
{'key': KeyCodes.VK_ESCAPE, 'release': False},
{'key': KeyCodes.VK_LCONTROL, 'release': 2},
{'key': KeyCodes.VK_LSHIFT, 'release': 2},
{'key': KeyCodes.VK_ESCAPE, 'release': 2},
{'x': 0, 'y': 0, 'data': 0, 'flags': MouseCodes.MOUSE_MOVE_ABSOLUTE},
{'key': KeyCode.VK_LCONTROL, 'release': False},
{'key': KeyCode.VK_LSHIFT, 'release': False},
{'key': KeyCode.VK_ESCAPE, 'release': False},
{'key': KeyCode.VK_LCONTROL, 'release': 2},
{'key': KeyCode.VK_LSHIFT, 'release': 2},
{'key': KeyCode.VK_ESCAPE, 'release': 2},
{'x': 0, 'y': 0, 'data': 0, 'flags': MouseCode.MOUSE_MOVE_ABSOLUTE},
)


Expand All @@ -47,7 +47,7 @@ def test_press():
-------
test the `press` function
"""
for key in KeyCodes.__dict__.values():
for key in KeyCode.__dict__.values():
if not isinstance(key, int):
continue

Expand Down

0 comments on commit d5565fc

Please sign in to comment.