Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

How to perform a key press 'Enter' in Python?

acupen
Explorer

How to perform a key press 'Enter' in Python?

acupen
Explorer
Explorer

After most of the operation, I have to press 'Enter' to finish, 'Enter' is not in a easy position to press.

Is that a way I can write a python script to perform a 'Enter' press? then binding it to 'SPACE', much more easy to press.

I have already write some shortcut in Python to make it easy to work.

 

 

For example, I just press a 'G' button then I can go back to home view. It's quite a easy command like below:

 

def command_execute(args: adsk.core.CommandEventArgs):
    futil.log(f"{CMD_NAME} Command Execute Event")

    ## Go HOME!!!!
    app.activeViewport.goHome()

 

Then set a shortcut 'g' in Fusion, then every time I press 'G', it go back to home view.

 

So how can I send a 'Enter' in python to Fusion interface?  imitate a key press 'Enter' then send it to Fusion to finish an operation?

 

The only information I got now is the keycode of 'Enter'. 

EnterKeyCode16777221Enter

 

Thanks in advanced if you can share some experience with me 🙂

acupen_0-1733406538800.png

 

0 Likes
Reply
Accepted solutions (1)
533 Views
3 Replies
Replies (3)

Jorge_Jaramillo
Collaborator
Collaborator

Hi,

 

The pynput package can be used to simulate keystrokes.

Here is a snipped code for the ENTER key:

from pynput.keyboard import Key, Controller
keyboard = Controller()

keyboard.press(Key.enter)
keyboard.release(Key.enter)

 

I wonder how are you going to trigger its execution: from where?  under which conditions?

 

Regards,

Jorge Jaramillo

 

0 Likes

Trigger a script you write in Fusion is easy.

 

1. Run it as Add-Ins,

acupen_0-1733446293755.png

 

2. inside your add-ins, you write several commands. Then 'Change Keyboard Shortcut...'

acupen_1-1733446394460.png

 

3. just assign a shortcut key for the command(underneath is a script) your write. Then it's done. 

During fusion running, every time you press 'g', you trigger your script once ...

acupen_2-1733446495000.png

 

 

0 Likes

Accepted solution

and Thanks for your reply.

I figure out a way to achieve what I want to do, beyond Fusion.

 

Just install a microsoft enhance tool:  PowerToys.

https://github.com/microsoft/PowerToys/releases/tag/v0.86.0

then you can switch any keypress with other key in a system level.

 

I just switch the 'Space' key with 'Enter', everything is done now. 

 

Thanks again for you help.

 

 

0 Likes