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

Send scan code on key down instead of key up #6

Merged
merged 3 commits into from
Mar 24, 2025

Conversation

dgrilawidbanana
Copy link
Contributor

This feels more natural to me.

This feels more natural to me.
@eeriemyxi
Copy link
Owner

eeriemyxi commented Mar 24, 2025

Can you try:

    loop = asyncio.get_running_loop()
    last_scan_code = None

    while True:
        try:
            event = await loop.run_in_executor(None, keyboard.read_event)
        except RuntimeError:
            await asyncio.sleep(0.1)
            continue

        log.debug("Got event: %s", event)

        if event.event_type is keyboard.KEY_UP:
            last_scan_code = None
            continue
        
        if event.scan_code == last_scan_code:
            continue
       
        last_scan_code = event.scan_code

        log.debug(f"Sending {event.scan_code=} from {event=}")
        await websocket.send(str(event.scan_code))

... And see if it works the same. I've checked the source code of keyboard and it sends either UP or DOWN event. So the first clause of the elif statement is redundant.

@eeriemyxi
Copy link
Owner

Ah, I never noticed that I made the listener not do their job properly. Now that I see, the same issue is in the Linux listener as well; I guess I'll patch that one when I am free.

@dgrilawidbanana
Copy link
Contributor Author

dgrilawidbanana commented Mar 24, 2025

I added if event.event_type is not keyboard.KEY_DOWN in imitation of the original, which used if event.event_type is not keyboard.KEY_UP instead of if event.event_type is keyboard.KEY_DOWN. If you'd like, I can move it to the second operand of and, so it will sometimes be short circuited and affect performance less. As it is impossible for both if statements to be executed, there is no difference between two if statements and an elif statement. Using elif is, to me, more readable and makes more sense. The else statement, however, is redundant.

@eeriemyxi
Copy link
Owner

eeriemyxi commented Mar 24, 2025

I've used the style presented here throughout rest of the code, doing it in another style here brings inconsistency. If you reproduce the same behaviour in your tests, I'd appreciate if you make a commit copying that style (I'm fine with doing it myself if necessary.)

Separated if statement, removed some newlines.
@eeriemyxi eeriemyxi merged commit 3cbf694 into eeriemyxi:main Mar 24, 2025
1 check passed
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.

2 participants