KeyEvent in layerscript?
Moderators: Víctor Paredes, Belgarath, slowtiger
-
- Posts: 32
- Joined: Tue Mar 24, 2015 3:04 pm
KeyEvent in layerscript?
Is it possible to use moho's keyEvent inside of a layerscript? Or does it solely work in tool scripts?
thanks in advance!
Jeroen
thanks in advance!
Jeroen
- hayasidist
- Posts: 3840
- Joined: Wed Feb 16, 2011 8:12 pm
- Location: Kent, England
Re: KeyEvent in layerscript?
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:
and in the tool
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
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
-
- Posts: 32
- Joined: Tue Mar 24, 2015 3:04 pm
Re: KeyEvent in layerscript?
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:
the layerscript:
It just prints: 'test', there is no keyboard interaction.
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
Code: Select all
function LayerScript(moho)
print(HS_dir)
end
-
- Posts: 32
- Joined: Tue Mar 24, 2015 3:04 pm
Re: KeyEvent in layerscript?
I think I must be missing something basic in the toolscript, for it to register the keyEvents.
- hayasidist
- Posts: 3840
- Joined: Wed Feb 16, 2011 8:12 pm
- Location: Kent, England
Re: KeyEvent in layerscript?
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
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_???
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
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_???

-
- Posts: 32
- Joined: Tue Mar 24, 2015 3:04 pm
Re: KeyEvent in layerscript?
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
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
-
- Posts: 32
- Joined: Tue Mar 24, 2015 3:04 pm
Re: KeyEvent in layerscript?
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.
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.
-
- Posts: 32
- Joined: Tue Mar 24, 2015 3:04 pm
Re: KeyEvent in layerscript?
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.
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: 3840
- Joined: Wed Feb 16, 2011 8:12 pm
- Location: Kent, England
Re: KeyEvent in layerscript?
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 ...
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 ...
-
- Posts: 32
- Joined: Tue Mar 24, 2015 3:04 pm
Re: KeyEvent in layerscript?
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.
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.
-
- Posts: 306
- Joined: Thu May 13, 2010 2:01 pm
Re: KeyEvent in layerscript?
Jeroen,
You can script button tools that set the global variables. Just make a tool script like this:
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:
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
Code: Select all
function LayerScript(moho)
if (DV_SetGlobal.OnOff) then print("on")
else print("off")
end
end
Re: KeyEvent in layerscript?
That reminded me a little programming trick I learned recently, the easiest way to invert a boolean.
Instead of this:
We can do this:
Instead of this:
Code: Select all
if (self.OnOff) then self.OnOff = false
else self.OnOff = true
end
Code: Select all
self.OnOff = not self.OnOff
________________________________________________________________________
https://mohoscripting.com/ - Unofficial Moho Lua scripting documentation
https://mohoscripts.com/ - The best place to publish and download scripts for Moho
https://mohoscripting.com/ - Unofficial Moho Lua scripting documentation
https://mohoscripts.com/ - The best place to publish and download scripts for Moho
-
- Posts: 32
- Joined: Tue Mar 24, 2015 3:04 pm
Re: KeyEvent in layerscript?
thanks breinmeester, I will try that!