KeyEvent in layerscript?

Moho allows users to write new tools and plugins. Discuss scripting ideas and problems here.

Moderators: Fahim, Distinct Sun, Víctor Paredes, erey, Belgarath, slowtiger

Post Reply
JeroenKoffeman
Posts: 32
Joined: Tue Mar 24, 2015 3:04 pm

KeyEvent in layerscript?

Post by JeroenKoffeman » Wed Mar 23, 2016 3:51 pm

Is it possible to use moho's keyEvent inside of a layerscript? Or does it solely work in tool scripts?

thanks in advance!
Jeroen
hayasidist
Posts: 2195
Joined: Wed Feb 16, 2011 8:12 pm
Location: Kent, England

Re: KeyEvent in layerscript?

Post by hayasidist » Wed Mar 23, 2016 4:59 pm

I can't see how it could -- the way you tell AS that you want to get keystrokes (that AS doesn't trap and use first so won't let you see) is to have a
function script:OnKeyDown(moho, keyEvent) -- but if you find a way to do this, please let me know!!

When I needed keyboard interaction with a layer script I used a "control" tool that set globals in response to keystrokes - and the layer script read those globals, for example in the layer script:

Code: Select all

	if HS_dir == "stop" then
		 v = 0
	elseif HS_dir == "L" then 
		v = - pace
	elseif HS_dir == "R" then 
		v =  pace
	end
and in the tool

Code: Select all

function HS_Control_harness:OnKeyDown(moho, keyEvent)
	local keyCode =  keyEvent.keyCode
	if     (keyCode == LM.GUI.KEY_LEFT) then
		HS_dir = "L"
	elseif (keyCode == LM.GUI.KEY_UP) then
		HS_dir = "stop"
	elseif (keyCode == LM.GUI.KEY_RIGHT) then
		HS_dir = "R"
	end
end
JeroenKoffeman
Posts: 32
Joined: Tue Mar 24, 2015 3:04 pm

Re: KeyEvent in layerscript?

Post by JeroenKoffeman » Wed Mar 23, 2016 5:48 pm

Hey, thanks. I had a hunch it could be done like this. However, I don't get a read on my key interactions yet. Can you see what I'm doing wrong in this toolscript?

the toolscript:

Code: Select all

ScriptName = "HS_Control_harness"

HS_Control_harness = {}

function HS_Control_harness:Name()
	return "asdadsadsa"
end

function HS_Control_harness:Version()
	return "1.0"
end

function HS_Control_harness:Description()
	return "asdasdsa"
end

function HS_Control_harness:Creator()
	return "dasdsads"
end

function HS_Control_harness:UILabel()
	return("system")
end



-- **************************************************
-- The guts of this script
-- **************************************************

HS_dir = "test"

function HS_Control_harness:OnKeyDown(moho, keyEvent)
   local keyCode =  keyEvent.keyCode
   if     (keyCode == LM.GUI.KEY_LEFT) then
      HS_dir = "L"
   elseif (keyCode == LM.GUI.KEY_UP) then
      HS_dir = "stop"
   elseif (keyCode == LM.GUI.KEY_RIGHT) then
      HS_dir = "R"
   end
end
the layerscript:

Code: Select all

function LayerScript(moho)

print(HS_dir)

end
It just prints: 'test', there is no keyboard interaction.
JeroenKoffeman
Posts: 32
Joined: Tue Mar 24, 2015 3:04 pm

Re: KeyEvent in layerscript?

Post by JeroenKoffeman » Wed Mar 23, 2016 6:28 pm

I think I must be missing something basic in the toolscript, for it to register the keyEvents.
hayasidist
Posts: 2195
Joined: Wed Feb 16, 2011 8:12 pm
Location: Kent, England

Re: KeyEvent in layerscript?

Post by hayasidist » Wed Mar 23, 2016 7:10 pm

couple of things:

somewhere I read that tools don't work if there's no Mouse event routines... I can't find the post about that (and I've never written one that doesn't have mouse routines!)-- but here's some "empty" mouse handling code I've stolen

Code: Select all


function HS_Test:OnMouseDown(moho, mouseEvent)
 end

function  HS_Test:OnMouseMoved(moho, mouseEvent)
end

function  HS_Test:OnMouseUp(moho, mouseEvent)
end
as I said, AS traps and refuses to pass on the keystrokes it thinks it needs .. you might not get L/R arrow -- for test purposes try LM.GUI.KEY_F1 / F2 / F3 (I can get those reliably)

and just to avoid any potential clashes, scripters are advised (but there's no registration / policing / ...) to choose and use their own prefix for script names and other global stuff -- I'm using HS_ ... and it's no biggie for this tryout, but when you get this flying you might like to pick your own prefix - maybe JK_??? :D
JeroenKoffeman
Posts: 32
Joined: Tue Mar 24, 2015 3:04 pm

Re: KeyEvent in layerscript?

Post by JeroenKoffeman » Wed Mar 23, 2016 7:18 pm

I can only get the OnKeyDown to register, when I add a function OnMouseDown to the toolscript.

But than it only works when the tool is selected in the tool panel. Also it doesn't register KEY_LEFT and KEY_RIGHT. It will only register KEY_LEFT/RIGHT in combination with cntrl, because this doesn't force the timeline to update.

Did you actually get your script to register KEY_LEFT and KEY_RIGHT without cntrl?

thanks in advance,

Jeroen
JeroenKoffeman
Posts: 32
Joined: Tue Mar 24, 2015 3:04 pm

Re: KeyEvent in layerscript?

Post by JeroenKoffeman » Wed Mar 23, 2016 7:22 pm

ha, you beat me to it. Sorry yes it was only for test purposes.

But as I understand from your answer, this will only work when the tool is selected? (This wouldn't be a big problem because I can still copy paste the code snipped to all the other tools)

The bigger issue would be: that it won't register LEFT and RIGHT, as your example gave me the hope that it would. Perhaps this is just not possible.
JeroenKoffeman
Posts: 32
Joined: Tue Mar 24, 2015 3:04 pm

Re: KeyEvent in layerscript?

Post by JeroenKoffeman » Wed Mar 23, 2016 7:26 pm

For some context: I'm working on a performance script that hides all unselected bonelayers temporarily when scrubbing through the timeline.

However, I still want to be able to jump through the timeline frame by frame (using the arrow keys) without it hiding anything.
hayasidist
Posts: 2195
Joined: Wed Feb 16, 2011 8:12 pm
Location: Kent, England

Re: KeyEvent in layerscript?

Post by hayasidist » Wed Mar 23, 2016 10:47 pm

L/R arrow No -- IMO there are WAY too many keystrokes that are trapped and not passed on by AS, and this issue is compounded by the ability to change shortcut assignments so there isn't a guarantee that whatever keystroke a script expects can actually be received by it. I've logged a formal request that keyEvent gives all keystrokes to the requesting script. It was listed for 11.2 but didn't really materialise. I'll bump it.

not sure if this is relevant but shift { and shift } will skip L/R through the *selected* keys in the timeline

hide unselected layers? e.g. if not the active layer then MohoLayer:SetVisible(false) ?

at first glance I don't see why it has to be a layer script but ...
JeroenKoffeman
Posts: 32
Joined: Tue Mar 24, 2015 3:04 pm

Re: KeyEvent in layerscript?

Post by JeroenKoffeman » Thu Mar 24, 2016 11:24 am

Thanks for your reply.

It has to be a layerscript, because it needs to update continuosly, no matter which tool is selected. What it basically does, is set all the bonelayers (except for the selected one), to IsEditOnly, whenever there was a timeline change, (if moho.frame ~= oldframe).
When moho.frame == oldframe, everything is turned visible again. This really helps boost performance in scenes with many rigs.

Thanks for the formal request. For now I will solve it by using the '<' and '>' keys instead of the arrow keys.
Breinmeester
Posts: 294
Joined: Thu May 13, 2010 2:01 pm

Re: KeyEvent in layerscript?

Post by Breinmeester » Mon Mar 28, 2016 12:11 pm

Jeroen,
You can script button tools that set the global variables. Just make a tool script like this:

Code: Select all

function DV_SetGlobal:Run(moho)
	if (self.OnOff) then self.OnOff = false
	else self.OnOff = true
end
and assign a shortcut button to it. Now pressing that button will toggle the DV_SetGlobal.OnOff between true and false. Simply read out that global as a condition for your layerscript:

Code: Select all

function LayerScript(moho)
if (DV_SetGlobal.OnOff) then print("on")
else print("off")
end
end
Stan
Posts: 132
Joined: Sun Apr 19, 2009 3:22 pm
Contact:

Re: KeyEvent in layerscript?

Post by Stan » Mon Mar 28, 2016 1:39 pm

That reminded me a little programming trick I learned recently, the easiest way to invert a boolean.
Instead of this:

Code: Select all

if (self.OnOff) then self.OnOff = false
    else self.OnOff = true
end
We can do this:

Code: Select all

self.OnOff = not self.OnOff
________________________________________________________________________
http://mohoscripting.com/ - Unofficial Moho (Anime Studio) Lua Scripting Documentation
http://2danimator.ru/ - Russian Anime Studio Forum
JeroenKoffeman
Posts: 32
Joined: Tue Mar 24, 2015 3:04 pm

Re: KeyEvent in layerscript?

Post by JeroenKoffeman » Mon Apr 25, 2016 10:49 am

thanks breinmeester, I will try that!
Post Reply