This is something I have been meaning to add to my thread on writing plugins for noobs. Key press handling is a bit awkward in LotRO's implementation because the key events are not actually fired by key presses (we can not actually detect the pressing of a specific key), rather they are fired when the game engine performs a specific action, such as toggling the HUD. These actions are not hardwired to a specific key, but are associated with keys through the Keybinding section of the game Options. So, what you are really looking for is how to hide your window in response to the "Toggle HUD" action (which is by default associated with F12, but end users can remap the key mapping). While this may take a bit of getting used to, it prevents needing to know what the key bindings are. I just wish the events had been more appropriately named, such as "control.Action" rather than the confusing "control.KeyDown"
To do this, you first have to have a control that has key events enabled. Below is a simple example with a window control called sampleWindow. In addition to not being able to detect an actual key stroke, we also can not detect the HUD state so we have to assume that the HUD is displayed when the plugin is loaded and track the state from there ourselves. I also include the related ability to hide the window in response to the Escape event (so called since it is usually bound to the Esc key). Notice that the Escape event is defined in the Turbine.UI.Lotro.Action enumeration but the Hud toggle event is not.
Code:
hudVisible=true; -- the actual HUD state is not exposed to Lua so we have to assume the HUD is visible when the plugin loads
sampleWindowVisible=true; -- used to retain the state of the window for when the HUD is toggled back on
sampleWindow=Turbine.UI.Lotro.Window()
sampleWindow.KeyDown=function(sender, args)
if ( args.Action == Turbine.UI.Lotro.Action.Escape ) then -- hide the window due to Escape
sampleWindow:SetVisible( false );
elseif ( args.Action == 268435635 ) then -- toggle HUD (the HUD action is not defined in the Turbine.UI.Lotro.Action enumeration although it should be)
hudVisible=not hudVisible;
if hudVisible then
sampleWindow:SetVisible(sampleWindowVisible);
else
sampleWindowVisible=sampleWindow:IsVisible();
sampleWindow:SetVisible(false);
end
end
end
sampleWindow:SetWantsKeyEvents(true); -- enable keyevents (Actions) for this control
You can also just open the main.lua in most of my plugins and search for the key word "Escape" which will be at the beggining of the action event handler.