from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
import typerScript
class MainScreen(FloatLayout):
def type(self, text):
typerScript.openNotepad()
typerScript.sendKeys(text)
class TyperApp(App): # define Base Class of Kivy App
def build(self):
root_widget = MainScreen()
return root_widget
# run program
if __name__ == '__main__':
TyperApp().run()
#:kivy 1.11.1
<MainScreen>
BoxLayout:
orientation: 'vertical'
TextInput:
id: text_to_type
text: 'hello world'
Button:
text: 'Type'
on_release: root.type(text_to_type.text)
import os
from pynput.keyboard import Key, Controller
import subprocess
import time
import win32com.client as comclt
keyboard = Controller()
wsh = comclt.Dispatch('WScript.Shell')
def openNotepad():
# check if notepad is open
print('checking for notepad')
r = os.popen('tasklist').read()
# open notepad if not opened
if 'notepad.exe' not in r:
print('opening notepad')
subprocess.Popen('notepad', stdin = subprocess.PIPE, shell = True)
time.sleep(0.5)
print('notepad is open')
def sendKeys(text):
# activate notepad
print('activating notepad...')
wsh.AppActivate('無題') # name of new Notepad window in Japanese OS
time.sleep(0.5)
# send keystrokes
print('sending keystrokes...')
keyboard.press(Key.f5)
keyboard.release(Key.f5)
wsh.SendKeys('{Enter}' + text + '{Enter}')
if __name__ == '__main__':
text_to_type = 'hello world'
openNotepad()
sendKeys(text_to_type)
I have not tried to run your code but here are a few ideas.
While I don’t think this is related to your problem, you should change the name of the type() method. It conflicts with the built-in type().
In sendKeys check for Notepad prior to activating.
Replace your sleep calls using Clock callbacks.
See: https://kivy.org/doc/stable/api-kivy.clock.html?highlight=clock#module-kivy.clock
--
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/3df20bef-5c9e-4a58-a4ec-402c340231b7%40googlegroups.com.
time.sleep(0.5)
d with a callback on completion.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
import typerScript
class MainScreen(FloatLayout):
def openAndType(self, text):
##typerScript.openNotepad() <- deleted
typerScript.sendKeys(text)
class TyperApp(App): # define Base Class of Kivy App
def build(self):
root_widget = MainScreen()
return root_widget
# run program
if __name__ == '__main__':
TyperApp().run()
#:kivy 1.11.1
<MainScreen>
BoxLayout:
orientation: 'vertical'
TextInput:
id: text_to_type
text: 'hello world'
Button:
text: 'Type'
on_release: root.openAndType(text_to_type.text)
import os
from pynput.keyboard import Key, Controller
import subprocess
import time
import win32com.client as comclt
keyboard = Controller()
wsh = comclt.Dispatch('WScript.Shell')
def openNotepad():
# check if notepad is open
print('checking for notepad')
r = os.popen('tasklist').read()
# open notepad if not opened
if 'notepad.exe' not in r:
print('opening notepad')
subprocess.Popen('notepad', stdin = subprocess.PIPE, shell = True)
##time.sleep(0.5) <- removed
print('notepad is open')
def sendKeys(text):
# open Notepad if not open
openNotepad() # <- added
# activate notepad
print('activating notepad...')
wsh.AppActivate('無題') # name of new Notepad window in Japanese OS
time.sleep(0.5)
# send keystrokes
print('sending keystrokes...')
keyboard.press(Key.f5)
keyboard.release(Key.f5)
wsh.SendKeys('{Enter}' + text + '{Enter}')
if __name__ == '__main__':
text_to_type = 'hello world'
##openNotepad() <- removed
sendKeys(text_to_type)
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
import typerScript
class MainScreen(FloatLayout):
def openAndType(self, text):
typerScript.text_to_type = text
typerScript.openNotepad()
class TyperApp(App):
def build(self):
root_widget = MainScreen()
return root_widget
if __name__ == '__main__':
TyperApp().run()
#:kivy 1.11.1
<MainScreen>
BoxLayout:
orientation: 'vertical'
TextInput:
id: text_to_type
text: 'hello world'
Button:
text: 'Type'
on_release: root.openAndType(text_to_type.text)
from kivy.clock import Clock
import os
from pynput.keyboard import Key, Controller
import subprocess
import time
import win32com.client as comclt
keyboard = Controller()
wsh = comclt.Dispatch('WScript.Shell')
text_to_type = ''
def openNotepad():
# check if notepad is open
print('checking for notepad')
r = os.popen('tasklist').read()
# open notepad if not opened
if 'notepad.exe' not in r:
print('opening notepad')
subprocess.Popen('notepad', stdin = subprocess.PIPE, shell = True)
if __name__ == '__main__':
time.sleep(1)
my_callback_1(0)
else:
Clock.schedule_once(my_callback_1, 1)
else:
print('notepad is open')
if __name__ == '__main__':
my_callback_1(0)
else:
Clock.schedule_once(my_callback_1, 0)
def my_callback_1(dt):
print('Callback 1 called', dt)
print('activating notepad...')
wsh.AppActivate('無題') # name of new Notepad window in Japanese OS
if __name__ == '__main__':
time.sleep(1)
my_callback_2(0)
else:
Clock.schedule_once(my_callback_2, 1)
def my_callback_2(dt):
print('Callback 2 called', dt)
sendKeys(text_to_type)
def sendKeys(text):
# send keystrokes
print('sending keystrokes...')
keyboard.press(Key.f5)
keyboard.release(Key.f5)
wsh.SendKeys('{Enter}' + text + '{Enter}')
if __name__ == '__main__':
text_to_type = 'hello world'
openNotepad()
import threading
def send_commands():
# send keystrokes
def start_notepad(callback):
wsh.AppActivate('無題') # name of new Notepad window in Japanese OS
callback() # run send_commands() after activate is done.
def open_notepad():
# get r
if 'notepad.exe' not in r:
threading.Thread(target=start_notepad,args=(send_commands)).start()
else:
send_commands()
More generally spawing a shell is generally not portable, and a security risk. So it is a good habit to avoid doing this.