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 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
-
-
Did the input_changed method get called at all?
Honestly, it is not clear that the kb would get callbacks from an external keyboard -- i can't find anything in iOS docs that says it would. Since a kb extension is for converting user interaction into text, I can see reasons for not passing direct entry through a separate set of callbacks. Also, if the kb is not on screen, I'm not sure it is being called at all.
-
@JonB said:
Did the input_changed method get called at all?
No, not if I hid the onscreen keyboard. (At least, assuming playing a sound on each
input_changed()
call—which works with the software keyboard—should also work with the HW keyboard.) My bluetooth keyboard has a button to toggle it on and off, though the Apple keyboards do not have such a key. (I presume there’s a normal key combo I don’t know to do this, from the way other special keys work. Not the globe key—this toggle key on my keyboard, I believe, either tells iPadOS “show me the onscreen keyboard even with the Bluetooth keyboard active”, or it changes its HID/SPP presentation so it’s a secondary keyboard. I’m not sure.)But the crucial takeaway is that, even when the onscreen keyboard is visible and active, keypresses on the hardware keyboard do not trigger
input_changed()
.The only reason I think there really should be some way to interact with the hardware keyboard is that, when you switch keyboards to Pythonista or any other third-party software keyboard, the hardware keyboard goes to plain QWERTY—when I have it set up via Settings (General → Keyboard → Hardware Keyboard) to do Dvorak. Yet, when I’m using the default Apple keyboard setup, the onscreen keyboard is QWERTY but the hw keyboard remains in Dvorak. Literally at this moment I type this, I have the Apple standard onscreen keyboard showing QWERTY, but I’m typing in Dvorak. (And if I do peck at the screen, the keys come out as shown in QWERTY, not Dvorak. I have it set up that way, you can make the the software keyboard Dvorak too, if you so choose.)
Honestly, it is not clear that the kb would get callbacks from an external keyboard -- i can't find anything in iOS docs that says it would. Since a kb extension is for converting user interaction into text, I can see reasons for not passing direct entry through a separate set of callbacks. Also, if the kb is not on screen, I'm not sure it is being called at all.
That would comport exactly with the behavior I’m seeing. That would really be disappointing. It would explain, though, why there are no hardware-kb-compatible keyboards on the App Store for things like unusual/custom keymaps or for languages Apple doesn’t directly support, like many Native American languages and Esperanto—if the OS doesn’t support it at all for App Store keyboards, Pythonista can’t do it either.