2018-10-19 17:57:25 +00:00
|
|
|
import PySimpleGUI as sg
|
|
|
|
|
|
|
|
|
2018-10-29 00:01:03 +00:00
|
|
|
class keyboard():
|
|
|
|
def __init__(self, font=('Arial', 16)):
|
|
|
|
self.font = font
|
|
|
|
numberRow = '1234567890'
|
|
|
|
topRow = 'QWERTYUIOP'
|
|
|
|
midRow = 'ASDFGHJKL'
|
|
|
|
bottomRow = 'ZXCVBNM'
|
|
|
|
keyboard_layout = [[sg.Button(c, key=c, pad=(0, 0), size=(4, 2), font=self.font) for c in numberRow] + [
|
|
|
|
sg.Button('⌫', key='back', pad=(0, 0), size=(4, 2), font=self.font),
|
|
|
|
sg.Button('Esc', key='close', pad=(0, 0), size=(4, 2), font=self.font)],
|
|
|
|
[sg.T(' ' * 4)] + [sg.Button(c, key=c, pad=(0, 0), size=(4, 2), font=self.font) for c in
|
|
|
|
topRow],
|
|
|
|
[sg.T(' ' * 11)] + [sg.Button(c, key=c, pad=(0, 0), size=(4, 2), font=self.font) for c in
|
|
|
|
midRow],
|
|
|
|
[sg.T(' ' * 18)] + [sg.Button(c, key=c, pad=(0, 0), size=(4, 2), font=self.font) for c in
|
|
|
|
bottomRow]]
|
2018-10-19 17:57:25 +00:00
|
|
|
|
2018-10-29 00:01:03 +00:00
|
|
|
self.window = sg.Window('keyboard',
|
|
|
|
grab_anywhere=True,
|
|
|
|
keep_on_top=True,
|
|
|
|
alpha_channel=0,
|
|
|
|
location=(850,350),
|
|
|
|
no_titlebar=True,
|
|
|
|
).Layout(keyboard_layout).Finalize()
|
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:
|
|
|
|
self.focus.Update(self.focus.Get() + self.event)
|
|
|
|
elif self.event == 'back':
|
|
|
|
Text = self.focus.Get()
|
|
|
|
if len(Text) > 0:
|
|
|
|
Text = Text[:-1]
|
|
|
|
self.focus.Update(Text)
|
|
|
|
|
|
|
|
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):
|
|
|
|
self.event, _ = self.window.Read(timeout=0)
|
|
|
|
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):
|
|
|
|
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')],
|
|
|
|
[sg.Input(size=(17, 1), key='input1', do_not_clear=True)],
|
|
|
|
[sg.InputText(size=(17, 1), key='input2', do_not_clear=True)],
|
|
|
|
[sg.Button('on-screen keyboard', key='keyboard')],
|
|
|
|
[sg.Button('close', key='close')]]
|
2018-10-19 17:57:25 +00:00
|
|
|
|
2018-10-29 00:01:03 +00:00
|
|
|
self.mainWindow = sg.Window('On-screen test',
|
|
|
|
grab_anywhere=True,
|
|
|
|
no_titlebar=False,
|
|
|
|
).Layout(layout).Finalize()
|
|
|
|
self.keyboard = keyboard()
|
|
|
|
self.focus = None
|
2018-10-19 17:57:25 +00:00
|
|
|
|
2018-10-29 00:01:03 +00:00
|
|
|
def run(self):
|
|
|
|
while True:
|
|
|
|
cur_focus = self.mainWindow.FindElementWithFocus()
|
|
|
|
if cur_focus is not None:
|
|
|
|
self.focus = cur_focus
|
|
|
|
event, values = self.mainWindow.Read(timeout=200, timeout_key='timeout')
|
|
|
|
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()
|