Dup Ver Goto 📝

Send Keys Via OSC

PT2/music/daw/reaper/osc reaper osc does not exist
To
74 lines, 257 words, 2287 chars Page 'KeySender' does not exist.

The idea here is that, since I have many spare old Thinkpads lying around, I can use these as extra shortcut keys. I can, for example, plug a wireless keyboard into the Thinkpad, and then use this script. So these keys cause OSC messages to be sent to Reaper which can then be bound to actions.

#!/usr/bin/env python

from PySide6.QtCore import *
from PySide6.QtGui import *
from PySide6.QtWidgets import *
from pythonosc import udp_client

import sys
import socket
def get_ip_for_host(host,family=socket.AF_INET):
  try:
    addrinfo = socket.getaddrinfo(host,0,family=family,type=socket.SOCK_DGRAM)
    if len(addrinfo) == 0:
      print(f"No info for {host}",file=sys.stderr)
      return None
    return addrinfo[0][4][0]
  except socket.gaierror as e:
    print(f"getaddrinfo failed for {host} : {e}",file=sys.stderr)
    return None

# ensure this is the IP address in Reaper preferences:
# 127.0.0.1 won't work, nor will an IPV6 address.
# get these from the command line or a config file or something.
host = "machine.reaper.is.running.on"
port = 8000

class SendKey:
  def __init__(self,host,port,chan=999):
    self.chan = chan
    self.host = host
    self.port = port
    self.ip = get_ip_for_host(self.host)
    self.client = udp_client.SimpleUDPClient(self.ip,self.port)
  def send(self,k):
    addr = f"/hh/{self.chan}/key/{k}"
    self.client.send_message(addr,[])

class Win(QWidget):
  def __init__(self,content="Key To OSC"):
    super().__init__()
    self.sendkey = SendKey("behemoth",2808)
    self.resize(640,400)
    self.label = QLabel(self)
    self.label.setText(content)
  def keyPressEvent(self,event):
    key = event.key()
    key = QKeySequence(key).toString().lower()
    if not key in "abcdefghijklmnopqrstuvwxyz0123456789" and\
        not key in ["left","right","up","down"] and\
        not key in "!\"£$%^&*()_+-=[]{};:'#@~,./<>?\\|`¬":
      return
    modifiers = event.modifiers()
    if modifiers & Qt.ShiftModifier:
      key = "S-"+key
    if modifiers & Qt.MetaModifier:
      key = "M-"+key
    if modifiers & Qt.ControlModifier:
      key = "C-"+key
    if modifiers & Qt.AltModifier:
      key = "A-"+key
    if key == "C-q":
      app.quit()
    self.sendkey.send(key)

app = QApplication([])
win = Win()
win.show()
app.exec()