SOLVED WASD instead of arrow keys?



  • Hi! Is there a way to change my Robofont preferences to make the wasd keys work as my arrow keys? I feel like this could really speed up my workflow!

    Not sure how to do this if not in preferences, I am new to python scripts and drawbot, etc.
    Let me know if you have any ideas!



  • I updated it based on @frederik's comments and added a few objects:

    from vanilla import FloatingWindow
    from mojo.events import addObserver, extractNSEvent
    
    class WASDTool:
    
        def __init__(self):
            addObserver(self, "keyWasPressed", "keyDown")
    
        def keyWasPressed(self, info):
            glyph = info["glyph"]
            if glyph:
    
                event = info["event"]
                characters = event.characters()
                shiftDown = extractNSEvent(info)['shiftDown']
                commandDown = extractNSEvent(info)['commandDown']
                
                x = 1
                if shiftDown:
                    x = 5
                    if commandDown:
                        x = 100
                        
                KEYMAPPING = {
                    "w":(0,x),
                    "a":(-x,0),
                    "s":(0,-x),
                    "d":(x,0),
                }
    
                objects = []
                objects.extend(glyph.selectedBPoints)
                objects.extend([p for p in glyph.selectedPoints if p.type == "offcurve"])
                objects.extend(glyph.selectedComponents)
                objects.extend(glyph.selectedAnchors)
                
                if objects != []:
                    # convert to lowercase so we dont have to use a huge dict
                    if characters.lower() in KEYMAPPING.keys():
                        move = KEYMAPPING[characters.lower()]
                        if move:
                            glyph.prepareUndo("movePoints")
                            for obj in objects:
                                obj.moveBy(move)
                            
                            glyph.changed()    
                            glyph.performUndo()
    
    WASDTool()
    


  • Cool idea and script! Thanks for sharing.

    @frederik, would glyph = info["glyph"] replace these two lines (and the indent), or am I understanding?

    @connor said in WASD instead of arrow keys?:

    self.glyph = CurrentGlyph()
    if self.glyph:



  • Ahhh thanks for the tips! I always forget that info has a “glyph”.


  • admin

    some small notes:

    • the event info dictionary has a key glyph which is the current glyph: glyph = info["glyph"]
    • dont keep a reference to the glyph object in self.glyph this will just clutter your memory


  • You're very welcome @RachaelMiller and thanks @frederik :)



  • @connor Oh my goodness thank you so much Ill try it out!


  • admin

    nice example Connor!



  • Hey, @RachaelMiller! I wrote something pretty close a while back and tweaked a few things. It doesn't work if you have any other shortcuts on any of these keys so be careful.

    from vanilla import FloatingWindow
    from mojo.events import addObserver, extractNSEvent
    
    class WASDTool:
    
        def __init__(self):
            self.glyph = None
            addObserver(self, "keyWasPressed", "keyDown")
    
        def keyWasPressed(self, info):
            self.glyph = CurrentGlyph()
            if self.glyph:
                
                event = info["event"]
                characters = event.characters()
                shiftDown = extractNSEvent(info)['shiftDown']
                commandDown = extractNSEvent(info)['commandDown']
                
                x = 1
                if shiftDown:
                    x = 5
                    if commandDown:
                        x = 100
                        
                KEYMAPPING = {
                    "w":(0,x),
                    "a":(-x,0),
                    "s":(0,-x),
                    "d":(x,0),
                }
    
                points = self.glyph.selectedBPoints
                
                if points:
                    # convert to lowercase so we dont have to use a huge dict
                    if characters.lower() in KEYMAPPING.keys():
                        move = KEYMAPPING[characters.lower()]
                        if move:
                            self.glyph.prepareUndo("movePoints")
                            for bPoint in points:
                                bPoint.moveBy(move)
                            
                            self.glyph.changed()    
                            self.glyph.performUndo()
    
    WASDTool()
    

Log in to reply