2 Pitchfork Music Player Daemon Client
3 Copyright (C) 2007 Roger Bystrøm
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; version 2 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 var RETURN_KEY_CODE = 13; // enter...
21 var keyboard_listeners;
22 var keyboard_elem_count = 0;
24 var KEYBOARD_NO_KEY = 0;
25 var KEYBOARD_CTRL_KEY = 1;
26 var KEYBOARD_ALT_KEY = 1 << 1;
27 var KEYBOARD_META_KEY = 1 << 2;
28 var KEYBOARD_SHIFT_KEY = 1 << 3;
30 var KEYBOARD_MATCH_ALL = "";
33 function KeyListener(callback_function, match_char, extra_keys, active) {
34 this.callback = callback_function;
35 this.matchChar = match_char;
36 this.extra_keys = extra_keys;
40 function keyboard_init() {
41 document.addEventListener("keydown", keyboard_input_handler_cb, false);
42 keyboard_listeners = new HashMap();
43 if(document.body.focus)
44 document.body.focus();
47 function keyboard_register_listener(callback_func, match_char, extra_keys, active) {
48 var kid = keyboard_elem_count++;
50 if(match_char==KEYBOARD_MATCH_ALL) {
52 keyboard_listeners.insert(kid, new KeyListener(callback_func, match_char, extra_keys, active));
55 match_char = match_char.toUpperCase().charCodeAt(0);
56 keyboard_listeners.put(kid, new KeyListener(callback_func, match_char, extra_keys, active));
62 function keyboard_set_active(id, active) {
64 keyboard_listeners.get(id).active = active;
67 debug("setting kb " + id + " to " + active + " failed");
71 function keyboard_remove_listener(key) {
72 keyboard_listeners.remove(key);
75 function keyboard_input_handler_cb(ev) {
78 //debug("got a key: " + (ev.shiftKey?"shift ":"") + (ev.ctrlKey?"ctrl ":"") + (ev.altKey?"alt ":"") + (ev.metaKey?"meta ":""));
80 if(ev.which&&ev.which>0)
82 else key = ev.keyCode; // hmm....
84 // this is ctrl+some key, yank it up by 64
85 if(ev.ctrlKey&&key>0&&key<27) {
89 // just match a-z and some special chars (search: ascii key table)
93 var num = KEYBOARD_NO_KEY;
95 num += KEYBOARD_CTRL_KEY;
97 num += KEYBOARD_SHIFT_KEY
99 num += KEYBOARD_ALT_KEY;
101 num += KEYBOARD_META_KEY;
102 var it = keyboard_listeners.iterator();
104 while((l = it.next())!=null) {
105 if(l.active && num == l.extra_keys && (l.matchChar == key||l.matchChar<0)) {