Need help with python script on windows
-
Hello,
I would like to make a simple page turner for musicians on my Microsoft Surface tablet.
The easiest way to do that would be to emulate a Right key when pressing the Flic button once and the Left key when pressing it twice, but my programming skills are close to zero.So far, I've managed to run the FlicSDK.exe server and pair my Flic button using the "new_scan_wizard.py" script. Every presses are detected by the "test_client.py" script.
I now tried to modify the test_client.py script in order to add keyboard support:
from pynput.keyboard import Key, Controller keyboard = Controller()
This should allow me to emulate a key press with:
keyboard.press(Key.right) keyboard.release(Key.right)
My test_client.py script looks like this right now:
import fliclib from pynput.keyboard import Key, Controller client = fliclib.FlicClient("localhost") keyboard = Controller() def got_button(bd_addr): cc = fliclib.ButtonConnectionChannel(bd_addr) cc.on_button_up_or_down = \ lambda channel, click_type, was_queued, time_diff: \ print(channel.bd_addr + " " + str(click_type)) cc.on_connection_status_changed = \ lambda channel, connection_status, disconnect_reason: \ print(channel.bd_addr + " " + str(connection_status) + (" " + str(disconnect_reason) if connection_status == fliclib.ConnectionStatus.Disconnected else "")) client.add_connection_channel(cc) def got_info(items): print(items) for bd_addr in items["bd_addr_of_verified_buttons"]: got_button(bd_addr) client.get_info(got_info) client.on_new_verified_button = got_button client.handle_events()
I would be very thankful if someone with Python knowledge could tell me how I can add this basic block:
If single_press then emulate_right_key elseif double_press then emulate_left_keyThank you!
-
@emil
Thank you for your help!
I got a syntax error but I managed to repair it with this code:def got_button(bd_addr): cc = fliclib.ButtonConnectionChannel(bd_addr) cc.on_button_single_or_double_click = \ lambda channel, click_type, was_queued, time_diff: \ keyboard.press(Key.right) if click_type == fliclib.ClickType.ButtonSingleClick else keyboard.press(Key.left) cc.on_connection_status_changed = \ lambda channel, connection_status, disconnect_reason: \ print(channel.bd_addr + " " + str(connection_status) + (" " + str(disconnect_reason) if connection_status == fliclib.ConnectionStatus.Disconnected else "")) client.add_connection_channel(cc)
It works perfectly and it seems I don't need to release the key.
But it seems Windows Universal Apps don't recognize key presses sent by Python... Key presses are recognized everywhere else. Anybody knows why or how to get around it?
The next step to have a fully functional Windows integration would be to make the script work with Eventghost. I will try either by capturing the keystroke event and by sending http requests to it's built-in webserver. I will let you know!
-
This post is deleted! -
I think something like the following for got_button should work:
def got_button(bd_addr): cc = fliclib.ButtonConnectionChannel(bd_addr) cc.on_button_single_or_double_click = \ lambda channel, click_type, was_queued, time_diff: \ if click_type == fliclib.ClickType.ButtonSingleClick: keyboard.press(Key.right) keyboard.release(Key.right) else: keyboard.press(Key.left) keyboard.release(Key.left) cc.on_connection_status_changed = \ lambda channel, connection_status, disconnect_reason: \ print(channel.bd_addr + " " + str(connection_status) + (" " + str(disconnect_reason) if connection_status == fliclib.ConnectionStatus.Disconnected else "")) client.add_connection_channel(cc)