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.


    How to get key pressing in scene module?

    Pythonista
    ui module scene module
    3
    40
    4663
    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.
    • yaley
      yaley last edited by

      Hi, i am a newbie and i have a problem.
      In my project, cannot detect am i pressing the key, if use ui.View.get_key_commands() and ui.View.key_command, it can only detect the keypress, not keydown

      So how???

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

        https://www.swiftbysundell.com/tips/handling-keyup-and-keydown-events/

        This is now possible in iOS 13.4, however, someone will need to translate this to ObjC. @cvp? @mikael? I can give it a shot, but not until next week.

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

          sorry, but that website was JS or Java…..

          yaley 1 Reply Last reply Reply Quote 0
          • JonB
            JonB last edited by

            It is swift, but it is possible to convert that to objc_util

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

              @JonB I would like to help but it seems to be a little bit too complex for me, I guess it is better to wait that you find the time to do it. Sorry, but happy to discover this new feature from Apple

              1 Reply Last reply Reply Quote 1
              • JonB
                JonB last edited by JonB

                I believe we just need to have a custom view controller that implements

                pressesBegan_withEvent(presses, event)
                

                Where presses is an NSSet of UIPresses. You can use presses.allObjects() to convert a NSSet to array...from.there, key.

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

                  E.g. presses.allObjects()[0].key().keyCode()

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

                    @JonB what is pressesBegan_withEvent(presses, event)

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

                      @yaley said

                      what is pressesBegan_withEvent(presses, event)

                      A method to implement to the custom UIViewController, this is a new standard method called when a key goes down

                      1 Reply Last reply Reply Quote 1
                      • yaley
                        yaley last edited by

                        @cvp so the whole code is like…. objc.ObjCClass("UIViewController").pressBegan_withEvent(presses, event)
                        that?

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

                          @yaley Ho no. This is not so simple. You have to create a custom UIViewController and configure the used methods and protocols. And one method could be the new one which should be called at each key down

                          You could check this topic where a such ViewController is used with a user method CustomViewController_touchesBegan_withEvent_

                          Only to show it is not so easy

                          1 Reply Last reply Reply Quote 1
                          • yaley
                            yaley last edited by

                            @cvp ok Thanks for your helping!

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

                              @yaley Sorry that I can't help more...

                              1 Reply Last reply Reply Quote 1
                              • yaley
                                yaley last edited by

                                @cvp oh sorry, i have another question, what are presses and event?
                                Is presses like [“a”, “b”, “c”] and is event like a function?

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

                                  @yaley presses is a set of UIPress objects and event is an UIPressesEvent object. Both are ObjectiveC classes

                                  1 Reply Last reply Reply Quote 1
                                  • cvp
                                    cvp @yaley last edited by cvp

                                    @yaley see Apple doc UIPressesEvent and UIPress

                                    1 Reply Last reply Reply Quote 1
                                    • yaley
                                      yaley last edited by

                                      @cvp thank you ❤️ you helped me a lot

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

                                        @yaley I hoped that some code like this could work, but not true. Thus, sure this is too complex for me. We will wait availability of @JonB

                                        from objc_util import *
                                        import ui
                                        
                                        UIViewController = ObjCClass('UIViewController')
                                        
                                        # Some callback definitions used by create_objc_class
                                        def pressesBegan_withEvent(_cmd, _presses, _event):
                                        	print('pressesBegan_withEvent')	
                                        	presses = ObjCInstance(_presses)
                                        
                                        # The main class...
                                        class MyView(ui.View):
                                        	def __init__(self):
                                        		super().__init__(self)
                                        		self.background_color = 'gray'
                                        
                                        	@on_main_thread
                                        	def initialize(self):
                                        		
                                        		v = ui.View()
                                        		v.frame = self.frame
                                        		tf = ui.TextField()
                                        		tf.frame = (10,10,200,32)
                                        		v.add_subview(tf)
                                        		vo = ObjCInstance(v)
                                        		#print(vo)
                                        		#print(dir(vo))
                                        
                                        		# set up the custom view controller
                                        		methods = [pressesBegan_withEvent]
                                        		protocols = []
                                        		CustomViewController = create_objc_class('CustomViewController', UIViewController, methods=methods, protocols=protocols)
                                        		cvc = CustomViewController.alloc().init()
                                        		cvc.view = vo
                                        		
                                        		# internal scheming...
                                        		self_objc = ObjCInstance(self)
                                        		self_objc.nextResponder().addChildViewController_(cvc)
                                        		self_objc.addSubview_(vo)
                                        		cvc.didMoveToParentViewController_(self_objc)
                                        		
                                        		tf.begin_editing()
                                        		
                                        
                                        if __name__ == '__main__':
                                            v = MyView()
                                            v.present('fullscreen')
                                            v.initialize()
                                        
                                        1 Reply Last reply Reply Quote 1
                                        • yaley
                                          yaley last edited by

                                          @cvp it doesn’t work….. but really thanks for your helping

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

                                            @yaley said

                                            it doesn’t work

                                            That's what I said with "it is not true" and I'm sorry for you but I'm sure that our guru, read @JonB, will solve that as soon he is available (dear John, sorry to put pressure on you)

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