schermetto: call command on up/down keypress

This commit is contained in:
boyska 2022-08-19 18:57:10 +02:00
parent 4d6169a2ad
commit ea09c335a8

View file

@ -4,6 +4,7 @@ import os
import sys
import argparse
import random
from subprocess import Popen
import gi
@ -45,6 +46,14 @@ def get_parser():
"this is useful to avoid CRT-burning; use <= 0 to disable"
),
)
p.add_argument("--on-key-press",
type=str,
default=None,
help="A program to call on up/down keypress. "
"It will be called with appropriate arguments "
"representing the action to take.")
return p
@ -113,6 +122,10 @@ class App(Gtk.Application):
def text(self, new_value: str):
self._text = new_value
def on_binding_pressed(self, action):
print('calling:', action)
Popen(self.cli_args.on_key_press.split() + [action])
def get_color(description: str) -> Gdk.RGBA:
rgba = Gdk.RGBA()
@ -143,6 +156,19 @@ class AppWindow(Gtk.ApplicationWindow):
self.app.connect("notify::text", self.on_text_changed)
if self.app.cli_args.on_key_press:
self.connect("key-press-event", self.on_key_press)
def on_key_press(self, widget, key: Gdk.EventKey):
if key.state != 0 or key.type != Gdk.EventType.KEY_PRESS:
return
if key.keyval == Gdk.KEY_Up:
self.app.on_binding_pressed('+1')
elif key.keyval == Gdk.KEY_Down:
self.app.on_binding_pressed('-1')
def force_redraw(self):
self.drawing_area.queue_draw()