Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider adding a curly bracket ("{}") check for the Key class #263

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

hiroshi-ya
Copy link

I'm working on a project that allows its users to customize their key configurations. Here's a snippet:

def move_forward(forwardKey : str) # forwardKey is a valid key
  ahk.send(forwardKey)
  ahk.key_down(forwardKey)
  sleep(1)
  ahk.key_up(forwardKey)

Since key strings such as 'up' and 'shift' need curly brackets ({}) when passed to ahk.send(), I simply give a pair of curly brackets to the forwardKey string no matter what they are (e.g. 'a' becomes '{a}', 'up' becomes '{up}').

Problem is, the Key.DOWN/UP property also automatically adds a pair of curly brackets, e.g.:

from ahk import AHK, keys
k = keys.Key(key_name='{up}')
print(k.DOWN) # prints {{up} down}

So, in the move_forward() function,

  1. if forwardKey == 'up', then ahk.send(forwardKey) will not work as expected (sends 'u' and 'p' instead of the up arrow key);
  2. if forwardKey == '{up}', then ahk.key_down(forwardKey) will not work because it's equivalent to ahk.send('{{up} down}').

This PR aims to fix this issue - probably not very gracefully :/

hiroshi-ya and others added 2 commits February 23, 2024 01:54
For property DOWN and UP, do not add curly brackets if self is already braced by curly brackets
@coveralls
Copy link

Coverage Status

coverage: 76.247% (-0.009%) from 76.256%
when pulling ac9a2ad on hiroshi-ya:Key.DOWN/UP-bracket-check
into f2d38c9 on spyoungtech:main.

@spyoungtech
Copy link
Owner

Thanks for this. Yeah, there's some known issues like this around the keys module when used in various ways. It's not documented in the README partially for this reason. For example, it may produce incorrect strings for use as hotkey triggers.

Likely, the keys module will be rewritten to support a more well-defined usage across various use cases.

In the meantime, we can of course improve the current implementation. I'll submit a small review and will be happy to merge with those changes.

Copy link
Owner

@spyoungtech spyoungtech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some small stylistic changes requested.

ahk/keys.py Show resolved Hide resolved
ahk/keys.py Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants