PDA

View Full Version : Catch every keys



Watchwolf
2009-08-05, 05:35
I use Enna which is a media center. I want to control it with my squeezebox controller. To do this I need to write an extension in enna to get a command from the network (this is not a problem) and to add a plugins in the squeezebox controller to send the command to enna.

I plan to do something simple :
- select the enna plugin
- every key are catch by the plugin and send to Enna except the key "home" which quit the plugin enna.

As you see I need to catch every key, these keys must not be managed by the controller (the key "back" should not quit the plugin !). Do you think I can do this ? Currently I don't know a lot about the development on the squeezebox controller but I don't need to do something complicate and this point is the big problem.

indifference_engine
2009-08-05, 06:01
I'm no expert either but I've done exactly what you describe in my forthcoming applet. Basically you need to use the window:addlistener() method to trap all the key presses. BTW, if you've disabled Back and Home how do you intend to get out of your applet to play any music?
I'll post my source code here when I get home if you like?

Watchwolf
2009-08-05, 08:12
I will be glad if you post your source code :)

I don't plan to override the key home.

indifference_engine
2009-08-06, 05:40
I'm no expert either but I've done exactly what you describe in my forthcoming applet. Basically you need to use the window:addlistener() method to trap all the key presses. BTW, if you've disabled Back and Home how do you intend to get out of your applet to play any music?
I'll post my source code here when I get home if you like?

might be a little while I'm afraid. By broadband connection has gone down due to a problem at the exchange. BT are working on it...

indifference_engine
2009-08-10, 16:33
ok, I'm back online so as promised...

You need to declare the various key and event constants (in capitals) and can get them from the wiki or by looking at other squeezeplay applets. I'm assuming your applet has a menu screen which your code references by the object variable 'window'. If that's the case then stick the following into your code that creates the menu.

window:addListener(EVENT_KEY_ALL,
function(event)
local keyCode = event:getKeyCode()
local type = event:getType()

if type = EVENT_KEY_PRESS then -- key has been pressed and released
if keyCode == KEY_GO then
-- do stuff if centre button is pressed.
elseif keyCode == KEY_BACK then
-- do stuff if back is pressed.
elseif keyCode == KEY_PLAY then
-- you get the idea...
elseif keyCde == KEY_HOME
-- this keeps the default action
return EVENT_UNUSED
end
elseif type == EVENT_KEY_HOLD -- key has been held
if keyCode == KEY_ADD then
-- do stuff if add is held
end
end
end
)


I must stress that I'm very new to creating applets so this may not be the best way (or even a good way) of doing it but it works for me. I'm also not 100% sure of how or why it works!
PS My applet will be released shortly so you'll be able too see the code for real!

indifference_engine
2009-08-18, 15:22
As promised, my applet is now available if you want to look at it...
http://forums.slimdevices.com/showthread.php?t=66795

After reading your post more closely, I'm not sure if it exemplifies what you're after as I was unable to capture the Back key, but I got all the others. The Back key does some weird stuff and seems to only fire the EVENT_KEY_* event after it has pushed the window off-screen by which time it's too late to do anything. The WINDOW_INACTIVE event may be a way round it but I didn't get very far with it.