Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
can pykeys see key presses from external keyboards?
-
I am trying to build a function where every time you press enter on external keyboard, it inserts some text at the beginning and end of the line before the app handles the enter key press. Is this impossible?
-
@jamm, it is possible if you are writing in a Pythonista ui.TextView. It is probably possible if you are using Pythonista custom keyboard. It is not possible in some random other app with a random keyboard.
What are you after?
-
I want to make it so when the keyboard is active it is inserting a tag before and after the text whenever I press return. I only use the external keyboard, never the on screen keyboard.
-
Are you talking about within pythonista, or in other apps?
If you are talking about within pythonista, You can implement textfield_did_change to change what is entered.
-
-
How can I debug that this is being called? The alert and insert_text functions don’t have an effect. The only thing that happens is it appends “o” only for key presses to the soft keyboard. I’m editing Text Effects.py
def textview_should_change(self, textview, range, replacement): keyboard.insert_text("r") dialogs.alert("s") return True def no_effect(text): keyboard.insert_text("p") dialogs.alert("q") return text + "o"
-
-
In another app I want it to form tags around the text every time enter is pressed. It seems impossible due to limitations.
-
@jamm I don't know with an external keyboard because I don't have it for testing but, with the virtual keyboard, I think it is possible, but I may be wrong
-
@jamm please try this quick and dirty script as custom keyboard in any app.
You will see automatic insert of BEF at begin and END at end of line where you tap return.#!python3 import keyboard import ui class KeyboardInfoView (ui.View): def __init__(self, *args, **kwargs): super().__init__(self, *args, **kwargs) self.background_color = '#00436e' def kb_should_insert(self, text): # You can modify the inserted text here: if ord(text) == 10: # return key pressed before,after = keyboard.get_input_context() keyboard.backspace(len(before)) keyboard.insert_text('BEF'+before+'END') return text if __name__ == '__main__': v = KeyboardInfoView() keyboard.set_view(v, mode='minimized')
-
Code is not fully ok.
In some apps, the "before" part of context is the entire line, in other apps, no.
Thus, code must be improved. Take it as first draft. -
A little bit better, no backspace, but still only ok if "before" = entire line before cursor
#!python3 import keyboard import ui class KeyboardInfoView (ui.View): def __init__(self, *args, **kwargs): super().__init__(self, *args, **kwargs) self.background_color = '#00436e' def kb_should_insert(self, text): # You can modify the inserted text here: if ord(text) == 10: # return key pressed before,after = keyboard.get_input_context() # hoping that before = entire line before cursor keyboard.move_cursor(-len(before)) keyboard.insert_text('BEF') keyboard.move_cursor(len(before)) keyboard.insert_text('END') #keyboard.backspace(len(before)) #keyboard.insert_text('BEF'+before+'END') return text if __name__ == '__main__': v = KeyboardInfoView() keyboard.set_view(v, mode='minimized')
-
Might be worth checking into key commands
http://omz-software.com/pythonista/docs/ios/ui.html#ui.View.key_command
-
@mcriley821 key_command is not called in this case and, anyway, does not allow to modify entry, and surely not in an other app than Pythonista
-
I was interested in this same question and created a forum account intending to ask it when I saw this thread already active.
So, I just tried the “BEF END” test script above (the latter one, “A little bit better), and it works as advertised in some programs (such as the Pythonista editor itself) when you tap “return” on the onscreen keyboard, except for one thing—it makes the text view jump down so that the typed text is offscreen.
In other apps (such as typing into this very text field in the forum using Safari), it only partially works with the software keyboard—it inserts BEF, but never END.
But with an external keyboard, it does absolutely nothing that I can see. Well, it does do one thing: my standard layout is Dvorak, but switching to Pythonista keyboard, the HW keyboard becomes QWERTY again.
I tried the key_command mentioned, too, and it is never called.
Btw, the problem I was hoping to solve was a multilingual keyboard—using switchable input methods like Vim and Emacs offer, or RFC 1345 encoding, letting me type multilingual text without having to learn the native keyboard layouts for each script.
-
@trey said:
—it makes the text view jump down so that the typed text is offscreen.
I don't have this problem, but don't forget that my little script was only written to show what could be possible, sure it is not bug free 🙄
-
-
@trey said:
switching to Pythonista keyboard, the HW keyboard becomes QWERTY again.
Normal, Pythonista keyboard is always QWERTY
-
@trey said:
with an external keyboard, it does absolutely nothing that I can see.
I can't test anything, I don't have any external keyboard
-