]> Joshua Wise's Git repositories - patchfork.git/blob - std/keyboard.js
Add support for displaying the year; put the album in italics. (Feature request...
[patchfork.git] / std / keyboard.js
1 /* 
2     Pitchfork Music Player Daemon Client
3     Copyright (C) 2007  Roger Bystrøm
4
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.
8
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.
13
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.
17 */
18
19 var RETURN_KEY_CODE = 13; // enter...
20
21 var keyboard_listeners;
22 var keyboard_elem_count = 0;
23
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;
29
30 var KEYBOARD_MATCH_ALL = "";
31
32
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;
37         this.active = active;
38 }
39
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();
45 }
46
47 function keyboard_register_listener(callback_func, match_char, extra_keys, active) {
48         var kid = keyboard_elem_count++;
49
50         if(match_char==KEYBOARD_MATCH_ALL) {
51                 match_char = -1;
52                 keyboard_listeners.insert(kid, new KeyListener(callback_func, match_char, extra_keys, active));
53         }
54         else {
55                 match_char = match_char.toUpperCase().charCodeAt(0);
56                 keyboard_listeners.put(kid, new KeyListener(callback_func, match_char, extra_keys, active));
57         }
58
59         return kid;
60 }
61
62 function keyboard_set_active(id, active) {
63         try {
64                 keyboard_listeners.get(id).active = active;
65         }
66         catch(e) {
67                 debug("setting kb " + id + " to " + active + " failed");
68         }
69 }
70
71 function keyboard_remove_listener(key) {
72         keyboard_listeners.remove(key);
73 }
74
75 function keyboard_input_handler_cb(ev) {
76         var key = -1;
77
78         //debug("got a key: " + (ev.shiftKey?"shift ":"") + (ev.ctrlKey?"ctrl ":"") + (ev.altKey?"alt ":"") + (ev.metaKey?"meta ":""));
79         
80         if(ev.which&&ev.which>0)
81                 key = ev.which;
82         else key = ev.keyCode; // hmm....
83
84         // this is ctrl+some key, yank it up by 64
85         if(ev.ctrlKey&&key>0&&key<27) {
86                 key+=64;
87         }
88
89         // just match a-z and some special chars (search: ascii key table)
90         if(key<32||key>90)
91                 return true;
92         
93         var num = KEYBOARD_NO_KEY;
94         if(ev.ctrlKey)
95                 num += KEYBOARD_CTRL_KEY;
96         if(ev.shiftKey)
97                 num += KEYBOARD_SHIFT_KEY
98         if(ev.altKey)
99                 num += KEYBOARD_ALT_KEY;
100         if(ev.metaKey)
101                 num += KEYBOARD_META_KEY;
102         var it = keyboard_listeners.iterator();
103         var l = null;
104         while((l = it.next())!=null) {
105                 if(l.active && num == l.extra_keys && (l.matchChar == key||l.matchChar<0)) {
106                         l.callback(ev);
107                         stop_event(ev);
108                         return false;
109                 }
110         }
111         return true;
112 }
This page took 0.030949 seconds and 4 git commands to generate.