
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi Fusion360 peeps,
I'm struggling with getting the keyboard input handler examples to work, I'm trying to trigger a message box in this test script, simply while the command is running, any key press should trigger 'MyKeyUpHandler' in a message box.
The script doesn't seem to error out but no Msgbox is triggered, please advise? I've tried everything I can think off but to no avail.
I'm just trying to get this example to work:
http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-8c89cd36-7d1b-4747-b643-c1784929427c
Eventually, while my command is running, I would like to switch the "active command" to various other commands like "line" "3 point arc" etc, while keeping this command running in the background. Hence "adsk.autoTerminate(False)". Any tips?
import adsk.core, adsk.fusion, adsk.cam, traceback, sys
# global mapping list of event handlers to keep them referenced for the duration of the command
handlers = []
cmdDefs = []
app = None
ui = None
# Event handler for the keyUp event.
class MyKeyUpHandler(adsk.core.KeyboardEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
eventArgs = adsk.core.KeyboardEventArgs.cast(args)
app = adsk.core.Application.get()
ui = app.userInterface
# Code to react to the event.
ui.messageBox('In MyKeyUpHandler event handler')
ui.messageBox(eventArgs.keyCode)
class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
global app, ui
app = adsk.core.Application.get()
ui = app.userInterface
# Get the command that was created.
cmd = adsk.core.Command.cast(args.command)
onKeyUp = MyKeyUpHandler()
# ui.messageBox(test)
cmd.keyUp.add(onKeyUp)
handlers.append(onKeyUp)
adsk.autoTerminate(False)
except:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def run(context):
global app, ui,handlers
handlers.clear()
try:
adsk.autoTerminate(False)
app = adsk.core.Application.get()
ui = app.userInterface
cmdDef = ui.commandDefinitions.itemById('cmdKeyboardEvent')
if not cmdDef:
# cmdDef = ui.commandDefinitions.addButtonDefinition('cmdInputsSample', 'Command Inputs Sample', 'Sample to demonstrate various command inputs.')
cmdDef = ui.commandDefinitions.addButtonDefinition('cmdKeyboardEvent', 'Command Inputs Sample', 'Sample to demonstrate various command inputs.')
# Connect to the command created event.
onCommandCreated = MyCommandCreatedHandler()
cmdDef.commandCreated.add(onCommandCreated)
handlers.append(onCommandCreated)
cmdDef.execute()
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def stop(context):
try:
global app, ui
app = adsk.core.Application.get()
ui = app.userInterface
for obj in cmdDefs:
if obj.isValid:
obj.deleteMe()
else:
ui.messageBox(str(obj) + ' is not a valid object')
handlers.clear()
ui.messageBox('Stop addin')
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Solved! Go to Solution.