2018-10-19 17:57:25 +00:00
|
|
|
import PySimpleGUI as sg
|
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
'''
|
|
|
|
Example of on-screen keyboard.
|
|
|
|
'''
|
2018-10-19 17:57:25 +00:00
|
|
|
|
2018-10-29 00:01:03 +00:00
|
|
|
class keyboard():
|
2018-12-08 18:14:13 +00:00
|
|
|
def __init__(self, location=(None, None), font=('Arial', 16)):
|
2018-10-29 00:01:03 +00:00
|
|
|
self.font = font
|
|
|
|
numberRow = '1234567890'
|
|
|
|
topRow = 'QWERTYUIOP'
|
|
|
|
midRow = 'ASDFGHJKL'
|
|
|
|
bottomRow = 'ZXCVBNM'
|
2018-12-08 18:14:13 +00:00
|
|
|
keyboard_layout = [[sg.Button(c, key=c, size=(4, 2), font=self.font) for c in numberRow] + [
|
|
|
|
sg.Button('⌫', key='back', size=(4, 2), font=self.font),
|
|
|
|
sg.Button('Esc', key='close', size=(4, 2), font=self.font)],
|
2019-10-23 20:10:03 +00:00
|
|
|
[sg.Text(' ' * 4)] + [sg.Button(c, key=c, size=(4, 2), font=self.font) for c in
|
|
|
|
topRow] + [sg.Stretch()],
|
|
|
|
[sg.Text(' ' * 11)] + [sg.Button(c, key=c, size=(4, 2), font=self.font) for c in
|
|
|
|
midRow] + [sg.Stretch()],
|
|
|
|
[sg.Text(' ' * 18)] + [sg.Button(c, key=c, size=(4, 2), font=self.font) for c in
|
|
|
|
bottomRow] + [sg.Stretch()]]
|
2018-10-19 17:57:25 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
self.window = sg.Window('keyboard', keyboard_layout,
|
|
|
|
grab_anywhere=True, keep_on_top=True, alpha_channel=0,
|
|
|
|
no_titlebar=True, element_padding=(0, 0), location=location, finalize=True)
|
2018-10-19 17:57:25 +00:00
|
|
|
self.hide()
|
|
|
|
|
2018-10-29 00:01:03 +00:00
|
|
|
def _keyboardhandler(self):
|
|
|
|
if self.event is not None:
|
|
|
|
if self.event == 'close':
|
|
|
|
self.hide()
|
|
|
|
elif len(self.event) == 1:
|
2019-10-23 20:10:03 +00:00
|
|
|
self.focus.update(self.focus.Get() + self.event)
|
2018-10-29 00:01:03 +00:00
|
|
|
elif self.event == 'back':
|
|
|
|
Text = self.focus.Get()
|
|
|
|
if len(Text) > 0:
|
|
|
|
Text = Text[:-1]
|
2019-10-23 20:10:03 +00:00
|
|
|
self.focus.update(Text)
|
2018-10-29 00:01:03 +00:00
|
|
|
|
|
|
|
def hide(self):
|
|
|
|
self.visible = False
|
|
|
|
self.window.Disappear()
|
2018-10-19 17:57:25 +00:00
|
|
|
|
2018-10-29 00:01:03 +00:00
|
|
|
def show(self):
|
|
|
|
self.visible = True
|
|
|
|
self.window.Reappear()
|
2018-10-19 17:57:25 +00:00
|
|
|
|
2018-10-29 00:01:03 +00:00
|
|
|
def togglevis(self):
|
|
|
|
if self.visible:
|
|
|
|
self.hide()
|
|
|
|
else:
|
|
|
|
self.show()
|
2018-10-19 17:57:25 +00:00
|
|
|
|
2018-10-29 00:01:03 +00:00
|
|
|
def update(self, focus):
|
2019-10-23 20:10:03 +00:00
|
|
|
self.event, _ = self.window.read(timeout=0)
|
2018-10-29 00:01:03 +00:00
|
|
|
if focus is not None:
|
|
|
|
self.focus = focus
|
|
|
|
self._keyboardhandler()
|
2018-10-19 17:57:25 +00:00
|
|
|
|
2018-10-29 00:01:03 +00:00
|
|
|
def close(self):
|
2019-10-23 20:10:03 +00:00
|
|
|
self.window.close()
|
2018-10-19 17:57:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GUI():
|
2018-10-29 00:01:03 +00:00
|
|
|
def __init__(self):
|
|
|
|
layout = [[sg.Text('Enter Text')],
|
2019-10-23 20:10:03 +00:00
|
|
|
[sg.Input('', size=(17, 1), key='input1')],
|
|
|
|
[sg.InputText('', size=(17, 1), key='input2')],
|
2018-10-29 00:01:03 +00:00
|
|
|
[sg.Button('on-screen keyboard', key='keyboard')],
|
|
|
|
[sg.Button('close', key='close')]]
|
2018-10-19 17:57:25 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
self.mainWindow = sg.Window('On-screen test', layout,
|
|
|
|
grab_anywhere=True, no_titlebar=False, finalize=True)
|
|
|
|
location = self.mainWindow.current_location()
|
2018-12-08 18:14:13 +00:00
|
|
|
location = location[0]-200, location[1]+200
|
|
|
|
self.keyboard = keyboard(location)
|
2018-10-29 00:01:03 +00:00
|
|
|
self.focus = None
|
2018-10-19 17:57:25 +00:00
|
|
|
|
2018-10-29 00:01:03 +00:00
|
|
|
def run(self):
|
|
|
|
while True:
|
2019-10-23 20:10:03 +00:00
|
|
|
cur_focus = self.mainWindow.find_element_with_focus()
|
2018-10-29 00:01:03 +00:00
|
|
|
if cur_focus is not None:
|
|
|
|
self.focus = cur_focus
|
2019-10-23 20:10:03 +00:00
|
|
|
event, values = self.mainWindow.read(
|
|
|
|
timeout=200, timeout_key='timeout')
|
2018-10-29 00:01:03 +00:00
|
|
|
if self.focus is not None:
|
|
|
|
self.keyboard.update(self.focus)
|
|
|
|
if event == 'keyboard':
|
|
|
|
self.keyboard.togglevis()
|
|
|
|
elif event == 'close' or event is None:
|
|
|
|
break
|
|
|
|
self.keyboard.close()
|
|
|
|
self.mainWindow.Close()
|
2018-10-19 17:57:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2018-10-29 00:01:03 +00:00
|
|
|
app = GUI()
|
|
|
|
app.run()
|