2

So I've put this script together, which allows me to send an osc, (open sound control), message whenever I hit the "w" key. (I need this to run in the background of my computer so any suggestions on how to make it more efficient would be appreciated!! :D)

The problem I've come to, is that I want it to send a different message when I hit "w" for the second time. (That's where the client.send_message comes in.) You'll see that I began to write this, to no avail lol.

In a nutshell, when I hit "w" the first time, it works perfectly. (Executes client.send_message("/TABS", "Mixer")). Now when I hit it the second time, I want it to execute client.send_message("/TABS", "Instruments") How can I go about doing this?

Code:

import argparse
import pynput

from pythonosc import udp_client
from pynput.keyboard import Key, Listener
from pynput import keyboard
import keyboard

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--ip", default="192.168.1.140",
        help="The ip of the OSC server")
    parser.add_argument("--port", type=int, default=8420,
        help="The port the OSC server is listening on")
    args = parser.parse_args()

    client = udp_client.SimpleUDPClient(args.ip, args.port)

    def on_press(key):
        if keyboard.is_pressed('w'):
            client.send_message("/TABS", "Mixer")
        else:
            client.send_message("/TABS", "Instruments")


    with Listener(on_press=on_press) as listener:
        listener.join()
martineau
  • 119,623
  • 25
  • 170
  • 301
maczook
  • 23
  • 2
  • 1
    Do you want the two actions to cycle back and forth with each press of "w", or do you want the "Mixer" only on the first press, and then "Instruments" on _all_ following presses? – b_c Feb 07 '20 at 21:01
  • @b_c Thanks for the reply! I'd like it to cycle back and forth – maczook Feb 07 '20 at 21:08

1 Answers1

1

Since you want to cycle the two operations back and forth, you can use itertools.cycle to do exactly that.

First, you'll want to define two small functions to do your two operations. In this case, I'm sending client as an argument to the functions.

def do_mixer(client_arg):
    client_arg.send_message("/TABS", "Mixer")

def do_instruments(client_arg):
    client_arg.send_message("/TABS", "Instruments")

Then, you can create a cycle object to endlessly switch between the two functions with each press to "w":

import argparse
import pynput

from pythonosc import udp_client
from pynput.keyboard import Key, Listener
from pynput import keyboard
import keyboard

# Additional import
from itertools import cycle

# New funcs
def do_mixer(client_arg):
    client_arg.send_message("/TABS", "Mixer")

def do_instruments(client_arg):
    client_arg.send_message("/TABS", "Instruments")

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--ip", default="192.168.1.140",
        help="The ip of the OSC server")
    parser.add_argument("--port", type=int, default=8420,
        help="The port the OSC server is listening on")
    args = parser.parse_args()

    client = udp_client.SimpleUDPClient(args.ip, args.port)

    # Function cycler
    func_cycler = cycle((do_mixer, do_instruments))

    def on_press(key):
        if keyboard.is_pressed('w'):
            # With each press of "w", call the function that is next in line
            # The cycle object will iterate the collection given to it infinitely
            # In this case, it will be: do_mixer, do_instruments, do_mixer, ...
            next(func_cycler)(client)

    with Listener(on_press=on_press) as listener:
        listener.join()
b_c
  • 1,202
  • 13
  • 24
  • So I tried adding the code you suggested, and I got a syntax error: " File "c:\Users\john\Desktop\test1.4.pyw", line 33 def on_press(key): ^ SyntaxError: invalid syntax" Is there a way I can share how I copied and pasted your code in? Sorry I'm new to the forum haha – maczook Feb 07 '20 at 21:19
  • @maczook only way would be an edit to your original post – b_c Feb 07 '20 at 21:23
  • ahh :( would you mind copying and pasting your code into the spots you'd put them in, and then re- answer with that code? :D hahahaha – maczook Feb 07 '20 at 21:28
  • Check the edits - I subbed it in with some comments :) – b_c Feb 07 '20 at 21:32
  • OH MY GOD!! YOU ARE SUCH A BOSS!! THANK YOU SO MUCH!!!! :D:D:D:D – maczook Feb 07 '20 at 21:34