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.
    • mikael
      mikael @jamm last edited by

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

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

        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.

        mikael 1 Reply Last reply Reply Quote 0
        • 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
                                            • First post
                                              Last post
                                            Powered by NodeBB Forums | Contributors