Flic Home

    Community

    • Login
    • Search
    • Popular
    • Users

    Need help with python script on windows

    Developers
    2
    4
    1238
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • erdnasyl
      erdnasyl last edited by

      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_key

      Thank you!

      1 Reply Last reply Reply Quote 0
      • erdnasyl
        erdnasyl @Emil last edited by

        @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!

        1 Reply Last reply Reply Quote 0
        • erdnasyl
          erdnasyl last edited by

          This post is deleted!
          1 Reply Last reply Reply Quote 0
          • Emil
            Emil FlicTeam last edited by

            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)
            
            erdnasyl 1 Reply Last reply Reply Quote 0
            • First post
              Last post