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()