omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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?

    Pythonista
    6
    29
    10488
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • JonB
      JonB last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • mikael
        mikael @jamm last edited by

        @jamm, as @JonB said, except look at the def textview_should_change(self, textview, range, replacement) method of the ui.TextView delegate, where you can see that the replacement is \n and can use the range to place the tags in the view.

        1 Reply Last reply Reply Quote 0
        • jamm
          jamm last edited by ccc

          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"
          
          cvp 1 Reply Last reply Reply Quote 0
          • cvp
            cvp @jamm last edited by

            @jamm As @JonB asked, do you want to write a custom keyboard for another app than Pythonista? As your code uses keyboard module, we could believe it.
            If you only want to intercept a keyboard key in a Pythonista app, in a TextView, then @mikael advice is ok.

            1 Reply Last reply Reply Quote 0
            • jamm
              jamm last edited by

              In another app I want it to form tags around the text every time enter is pressed. It seems impossible due to limitations.

              cvp 2 Replies Last reply Reply Quote 0
              • cvp
                cvp @jamm last edited by cvp

                @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

                1 Reply Last reply Reply Quote 0
                • cvp
                  cvp @jamm last edited by cvp

                  @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')
                  

                  1 Reply Last reply Reply Quote 0
                  • cvp
                    cvp last edited by

                    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.

                    1 Reply Last reply Reply Quote 0
                    • cvp
                      cvp last edited by cvp

                      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') 
                      
                      1 Reply Last reply Reply Quote 0
                      • mcriley821
                        mcriley821 last edited by

                        Might be worth checking into key commands

                        http://omz-software.com/pythonista/docs/ios/ui.html#ui.View.key_command

                        cvp 1 Reply Last reply Reply Quote 0
                        • cvp
                          cvp @mcriley821 last edited by

                          @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

                          1 Reply Last reply Reply Quote 0
                          • trey
                            trey last edited by trey

                            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.

                            cvp JonB 6 Replies Last reply Reply Quote 0
                            • cvp
                              cvp @trey last edited by

                              @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 🙄

                              1 Reply Last reply Reply Quote 0
                              • cvp
                                cvp @trey last edited by

                                @trey said:

                                —it inserts BEF, but never END

                                You're right, it is a bug more

                                1 Reply Last reply Reply Quote 0
                                • cvp
                                  cvp @trey last edited by

                                  @trey said:

                                  switching to Pythonista keyboard, the HW keyboard becomes QWERTY again.

                                  Normal, Pythonista keyboard is always QWERTY

                                  1 Reply Last reply Reply Quote 0
                                  • cvp
                                    cvp @trey last edited by

                                    @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

                                    trey 1 Reply Last reply Reply Quote 0
                                    • cvp
                                      cvp @trey last edited by

                                      @trey said:

                                      using switchable input methods

                                      Could you give an example, please

                                      1 Reply Last reply Reply Quote 0
                                      • JonB
                                        JonB @trey last edited by

                                        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.

                                        trey 1 Reply Last reply Reply Quote 0
                                        • trey
                                          trey @JonB last edited by trey

                                          @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.

                                          1 Reply Last reply Reply Quote 0
                                          • trey
                                            trey @cvp last edited by trey

                                            @cvp said:

                                            @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

                                            You really don’t have access to a single Bluetooth keyboard, nor a “Camera connection” dongle whose USB port you can use to hook up your desktop USB keyboard? You don’t need a special iPad or iOS-compatible keyboard.

                                            I’m speed throttled from replying to your multiple messages, so I’ll reply here:

                                            Could you give an example, please

                                            I did give three examples, in the words immediately after the ones you copy-pasted—“like Vim or Emacs offer, or RFC 1345 encoding”.

                                            If you mean how would it work, in RFC1345 encoding you type some character to enter RFC1345 encoding—ampersand is common—and then you type a sequence of from one to three ASCII characters that mnemonically get you a character elsewhere in Unicode space. For instance, to type θ, you press &h*—all the Greek letters are the matching letter followed by asterisk, so capital Θ is typed &H*. There are no ambiguous subsequences, so when you finish you immediately get the character and can continue typing ASCII or another sequence starting &.

                                            It’s slow for typing full texts in other languages, but for cases like International Phonetic Alphabet, math notation, or programming languages that use non-ASCII unicode, it’s quite handy. Even things like typing characters that require the ⌥ key on Macs into ASCII-only inputs.

                                            cvp 1 Reply Last reply Reply Quote 1
                                            • First post
                                              Last post
                                            Powered by NodeBB Forums | Contributors