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.


    Hardware keyboard input when scene is running

    Pythonista
    3
    4
    2199
    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.
    • KiloPasztetowej
      KiloPasztetowej last edited by

      Hey. I searched and searched but I couldn’t find definitive answer to the question, whether querying hardware keyboard state is possible in Pythonista when using scene. I know it can be done with UIKit but this doesn’t fit my needs. I love my iPad and I’d like to use it for some quick prototypes for my games but I just prefer keyboard input over touch.
      Any support would be welcome (events or just queries for keys)!

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

        @KiloPasztetowej, I assume that by UIKit support you are referring to the ui.View.get_key_commands and key_command(sender) methods.

        Since scene always runs in a
        UI view, you can create a custom ui.View that utilizes the above methods, and then use that as your scene’s view.

        Something like this – end with a two-finger swipe down:

        import ui
        import scene
        
        
        class MyKbHandler(ui.View):
            
            def __init__(self, my_scene, **kwargs):
                super().__init__(**kwargs)
                sceneview = scene.SceneView(
                    frame=self.bounds, flex='WH',
                )
                sceneview.scene = self.scene = my_scene
                self.add_subview(sceneview)
            
            def get_key_commands(self):
                return [
                    # Custom commands
                    { 'input': 'A' }
                ]
                
            def key_command(self, sender):
                # Process commands, e.g.:
                if sender['input'] == 'A':
                    self.scene.accelerate()
        
        
        class MyScene(scene.Scene):
            
            def setup(self):
                self.ship = scene.SpriteNode('spc:PlayerShip1Orange')
                self.ship.position = self.size / 2
                self.add_child(self.ship)
                
            def accelerate(self):
                x, y = self.ship.position
                self.ship.position = (x, y + 20)
                
        
        root = MyKbHandler(my_scene=MyScene())
        
        root.present('fullscreen', hide_title_bar=True)
        
        1 Reply Last reply Reply Quote 2
        • KiloPasztetowej
          KiloPasztetowej last edited by

          Great! Works like a charm and that’s exactly what I need. Thank you very much 🙂

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

            This post is deleted!
            1 Reply Last reply Reply Quote 0
            • First post
              Last post
            Powered by NodeBB Forums | Contributors