Get macroscript hotkey

posted in: Blog | 0

While working on my mScrub plugin to add new features and do some code cleanup I stumbled upon the need to get whatever hotkey the user assigns to the macro since in the current version it’s hard coded to use the Spacebar.

I did some research but couldn’t find a native way to do it with Maxscript so I ended up writing my own. I’ve tested it in 2014 and 2015 which use XML keyboard files. I’ve put .NET XML classes into play with a XPath query.

Since I’m also relying on .NET to check what keys are pressed, I’ve decided to return a System.Windows.Input.Key object instead of the virtual key (integer), but this is easy to convert back.

If you have any other way to do this, I would love to hear about it! Just leave it in the comments.

fn getMacroScriptHotkey macroName = (
	keyInterop  = dotNetClass "System.Windows.Input.KeyInterop"
	kbdFile = actionMan.getKeyboardFile()
	xmlDoc = dotNetObject "System.XML.XMLDocument" 
	xmlDoc.load kbdFile
	root = xmlDoc.selectNodes ("//shortcut[contains(@actionID,'" + macroName + "')]")
	if root.count == 1 then 
		xmlNode = root.itemOf[0]
		virtualKey = xmlNode.Attributes.ItemOf["accleleratorKey"].Value as integer
		return (keyInterop.keyFromVirtualKey virtualKey)
	else return undefined

And you can use it like this, also included is how to convert back to a virtual key using KeyInterop class.

hotKey = getMacroScriptHotkey "mScrub"

keyInterop  = dotNetClass "System.Windows.Input.KeyInterop"
keyInterop.VirtualKeyFromKey hotKey


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.