Detect keypress across 2 separate windows (main app + subprocess app)

65 views
Skip to first unread message

Tom Hartsock

unread,
Sep 9, 2022, 7:02:39 PM9/9/22
to Kivy users support
Hello,

I have a case where I need two separate Kivy windows, on two separate monitors, to display keyboard press events simultaneously.  I have a simple toy example of the code for 'app1.py' and 'app2.py' below (also attached).

Now since Kivy only allows one window per application, I had to use 'subprocess' on 'app2.py' to open the second window, and this solution works.  That is, when I click on the 'Press to open second window' button in my main application (i.e., app1)  a second window (i.e., app2) opens on my second monitor..

However, I am stuck as to how to simultaneously print to both application windows when the spacebar is pressed.  It works for app1 initially, and then app2 once it is opened, but it does not simultaneously print to both. 

Any ideas?

Best,
Tom

###############
# File name: app1.py

from kivy.app import App
from kivy.core.window import Window
from kivy.uix.floatlayout import FloatLayout
from kivy.lang import Builder
from kivy.properties import StringProperty
from subprocess import Popen

Window.size = (1920, 1080)
Window.borderless = True
Builder.load_string('''
<FirstWindow>:
id: _FirstWindow
FloatLayout:
Label:
text: "First Window"
size_hint: None, None
font_size: 50
pos: (900,940)
Label:
text: _FirstWindow.key_down
size_hint: None, None
font_size: 30
pos: (900,800)
Button:
text: "Press to open second window"
size_hint: None, None
font_size: 30
size: (450, 60)
pos: (720, 600)
on_press: root.OpenSecondWindow()
''')


class FirstWindow(FloatLayout):
key_down = StringProperty() # perform button state

def __init__(self, **kwargs):
super(FirstWindow, self).__init__(**kwargs)
self._keyboard = Window.request_keyboard(None, self)
self._keyboard.bind(on_key_down=self.on_keyboard_down, on_key_up=self.on_keyboard_up)

def OpenSecondWindow(self):
p = Popen(['python ./app2.py'], shell=True)

def on_keyboard_down(self, keyboard, keycode, text, modifiers):
if keycode[1] == 'spacebar':
self.key_down = 'spacebar pressed!'

def on_keyboard_up(self, keyboard, keycode):
if keycode[1] == 'spacebar':
self.key_down = ''


class App1(App):

def build(self):
return FirstWindow()


if __name__ == '__main__':
App1().run()


###############
# File name: app2.py

from kivy.config import Config
Config.set('graphics', 'resizable', '0')
Config.set('graphics', 'position', 'custom')
Config.set('graphics', 'top', '-900')
Config.set('graphics', 'left', '0')
Config.set('graphics', 'borderless', '1')
Config.set('graphics', 'fullscreen', 'auto')

from kivy.app import App
from kivy.core.window import Window
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import StringProperty
from kivy.lang import Builder

Window.size = (1920, 1080)
Window.borderless = True
Builder.load_string('''
<SecondWindow>:
id: _SecondWindow
FloatLayout:
Label:
text: "Second Window"
size_hint: None, None
font_size: 50
pos: (900,940)
Label:
text: _SecondWindow.key_down
size_hint: None, None
font_size: 30
pos: (900,800)
''')

class SecondWindow(FloatLayout):
key_down = StringProperty() # perform button state

def __init__(self, **kwargs):
super(SecondWindow, self).__init__(**kwargs)
self._keyboard = Window.request_keyboard(None, self)
self._keyboard.bind(on_key_down=self.on_keyboard_down, on_key_up=self.on_keyboard_up)

def on_keyboard_down(self, keyboard, keycode, text, modifiers):
if keycode[1] == 'spacebar':
self.key_down = 'spacebar pressed!'

def on_keyboard_up(self, keyboard, keycode):
if keycode[1] == 'spacebar':
self.key_down = ''


class App2(App):

def build(self):
return SecondWindow()


if __name__ == '__main__':
App2().run()


app1.py
app2.py

Elliot Garbus

unread,
Sep 10, 2022, 12:50:39 AM9/10/22
to kivy-...@googlegroups.com

Only the active window will receive key strokes.  I would consider using sockets (https://docs.python.org/3/library/socket.html) to send data between the 2 apps.

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/4c24779f-4adc-405f-85fe-0d14a80e180bn%40googlegroups.com.

 

Reply all
Reply to author
Forward
0 new messages