2 * (C) Copyright 2008 Jeremy Maitin-Shepard
4 * Use, modification, and distribution are subject to the terms specified in the
8 var conkeror = Components.classes['@conkeror.mozdev.org/application;1'].getService ().wrappedJSObject;
10 var last_keydown_event = null;
13 function handle_keydown(e) {
17 last_keydown_event = e;
20 function handle_keypress(e) {
24 // Ignore this keypress if we don't have a keycode
25 if (!last_keydown_event)
28 // Ignore this keypress if other modifiers are down
29 if (e.altKey || e.ctrlKey || e.metaKey)
32 // Ignore this keypress if we don't have a charcode
37 if (e.charCode == " ".charCodeAt(0))
40 var shift = e.shiftKey;
42 var keycode = last_keydown_event.keyCode;
43 var charcode = e.charCode;
45 add_mapping(keycode, shift, charcode, true);
48 var unshifted_keycode_to_charcode = [];
49 var shifted_keycode_to_charcode = [];
53 function add_mapping(keycode, shift, charcode, single) {
54 var table = shift ? shifted_keycode_to_charcode : unshifted_keycode_to_charcode;
56 var obj = table[keycode];
58 obj.element.parentNode.removeChild(obj.element);
61 var g = new conkeror.dom_generator(document, conkeror.XUL_NS);
62 var list = document.getElementById("mapping_list");
63 var item = g.element("listitem");
64 g.element("listcell", item, "label", keycode);
65 var keycode_name = conkeror.keycode_to_vk_name[keycode] || "";
66 g.element("listcell", item, "label", keycode_name);
67 g.element("listcell", item, "label", shift ? "true" : "false");
68 g.element("listcell", item, "label", charcode);
69 g.element("listcell", item, "label", String.fromCharCode(charcode));
70 list.appendChild(item);
71 table[keycode] = {charcode: charcode, element: item};
73 list.ensureElementIsVisible(item);
74 message("Added mapping: " + keycode + ((keycode_name.length > 0)? " (" + keycode_name + ")" : "") +
75 " -> " + charcode + " ('" + String.fromCharCode(charcode) + "')");
80 function update_count() {
81 document.getElementById("count").value = "Count: " + count;
86 var tables = [unshifted_keycode_to_charcode, shifted_keycode_to_charcode];
87 for each (let table in tables) {
88 for each (let x in table) {
89 x.element.parentNode.removeChild(x.element);
92 unshifted_keycode_to_charcode = [];
93 shifted_keycode_to_charcode = [];
95 message("Cleared mappings.");
98 function load_tables(tables) {
99 let [unshifted_table, shifted_table] = tables;
101 for (let x in unshifted_table) {
102 let y = unshifted_table[x];
104 add_mapping(x, false, y);
106 for (let x in shifted_table) {
107 let y = shifted_table[x];
109 add_mapping(x, true, y);
114 function load(use_defaults) {
115 let tables = conkeror.get_charcode_mapping_table_from_preferences();
116 if (tables != null) {
118 message("Loaded mappings.");
122 message("No mapping table stored in preferences. The default mapping table was loaded.");
125 message("Error: No mapping table stored in preferences.");
130 var unshifted_table = unshifted_keycode_to_charcode.map(function (x) x.charcode);
131 var shifted_table = shifted_keycode_to_charcode.map(function (x) x.charcode);
133 var str = [unshifted_table, shifted_table].toSource();
134 conkeror.user_pref("conkeror.charCodeMappingTable", str);
135 message("Saved mappings. Restart Conkeror for the new mapping table to take effect.");
139 let tables = conkeror.get_default_keycode_to_charcode_tables();
141 message("Loaded default mapping table.");
144 function clear_preference() {
146 conkeror.clear_pref("conkeror.charCodeMappingTable");
147 message("Cleared preference. Restart Conkeror for the default table to take effect.");
149 message("Preference was already not set.");
153 function message(msg) {
154 document.getElementById("message").value = msg;
158 window.addEventListener("keydown", handle_keydown, true);
159 window.addEventListener("keypress", handle_keypress, true);