timed Maya Hotkeys

I like the functionality of the space bar in Maya, hit it quickly and it swaps between single and quad view, but hold it and it shows you the hotbox and release and it leaves the panel views the same. I wanted to replicate this timed nature in some of my own hotkeys. It was actually pretty simple; using the PRESS and RELEASE hotkey settings and a very simple timer check you can change the command depending on the length of time the hotkey was held for.

Below is a sample function using this idea, you would just need to rename some stuff and fill in the parts that actually do something when the key is pressed or held. I’ve used this idea to create some extra functionality for hiding/showing, and also a modified fit to panel command that allows different levels of zoom when holding the button.


global proc timedHotkeyFunction()
{
	global float $timedHotkey_timer;

	//Use -1 to reset the timer on release
	if ( $timedHotkey_timer < 0 )
	{
		$timedHotkey_timer = `timerX`;
	}
	else
	{
		float $heldTime = `timerX -startTime $timedHotkey_timer`;
		$timedHotkey_timer = -1;

		//Resets the timer if the time is too long, this is to guard against
		//cases where the press and release get mixed round.
		if ( $heldTime > 10 )
		{
			$timedHotkey_timer = `timerX`;
			print "\nReseting Hotkey timer - held for too long!";
		}
		//Time to define a press or a hold, 0.3 seems to work quite well
		else if ( $heldTime < 0.3 )
		{
			print "\nPressed!";
			//******************************************************************
			//Code for pressed function here.
			//******************************************************************
		}
		else
		{
			print "\nHeld!";
			//******************************************************************
			//Code for hold function here.
			//******************************************************************
		}

	}
}

//Define the runTimeCommand that will show up in the hotkey editor, this command
//needs to be assigned to both the PRESS and RELEASE hotkeys
if ( !`runTimeCommand -q -ex timedHotkeyTest` )
{
	runTimeCommand
		-annotation "Set this to both PRESS and RELEASE on your hotkey"
		-command ("timedHotkeyFunction")
		timedHotkeyTest;
}