Permalink
Browse files

contrib/xrev: support osKey, simplify display, style, whitespace

  • Loading branch information...
1 parent d74e821 commit 27da1cadb21b476692c67d8a30d2b323866ddcd0 @retroj committed Feb 18, 2013
Showing with 54 additions and 45 deletions.
  1. +54 −45 contrib/xrev/content/xrev.js
@@ -1,5 +1,5 @@
/**
- * (C) Copyright 2008-2009 John J. Foerch
+ * (C) Copyright 2008-2009,2013 John J. Foerch
*
* Use, modification, and distribution are subject to the terms specified in the
* COPYING file.
@@ -11,29 +11,38 @@ const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
/* Generate vk name table */
var keycode_to_vk_name = [];
var vk_name_to_keycode = {};
-var name;
-var code;
-var prefix;
{
- prefix = "DOM_VK_";
- for (i in KeyEvent)
- {
+ var prefix = "DOM_VK_";
+ for (i in KeyEvent) {
/* Check if this is a key binding */
- if (i.substr(0, prefix.length) == prefix)
- {
- name = i.substr(prefix.length).toLowerCase();
- code = KeyEvent[i];
+ if (i.substr(0, prefix.length) == prefix) {
+ var name = i.substr(prefix.length).toLowerCase();
+ var code = KeyEvent[i];
keycode_to_vk_name[code] = name;
vk_name_to_keycode[name] = code;
}
}
}
+var special_modifiers = {
+ osKey: function (event) {
+ return ("getModifierState" in event &&
+ event.getModifierState("OS"));
+ }
+};
+
+var column_names = {
+ ctrlKey: "C",
+ metaKey: "M",
+ altKey: "A",
+ osKey: "s",
+ shiftKey: "S"
+};
+
// key_event_props: what properties of keyboard events do we care about?
var key_event_props = [
- 'type', 'charCode', 'keyCode', 'altKey',
- 'ctrlKey', 'shiftKey', 'metaKey',
- 'isChar', 'which'];
+ 'type', 'charCode', 'keyCode', 'which', 'ctrlKey',
+ 'metaKey', 'altKey', 'osKey', 'shiftKey'];
function quit () {
var appStartup = Components.classes["@mozilla.org/toolkit/app-startup;1"]
@@ -60,75 +69,75 @@ function key_event_handler (event) {
event.stopPropagation();
var table = document.getElementById('event-table');
- var row = document.createElementNS(XUL_NS,"listitem");
+ var row = document.createElementNS(XUL_NS, "listitem");
+
+ function get_field (k) {
+ if (k in special_modifiers)
+ var v = special_modifiers[k](event);
+ else
+ v = event[k];
+ if (typeof v == "boolean")
+ return (v ? (column_names[k] || true) : "");
+ else
+ return v;
+ }
function addcell (text) {
- var cap = document.createElementNS(XUL_NS,"listcell");
+ var cap = document.createElementNS(XUL_NS, "listcell");
cap.setAttribute("label", text);
row.appendChild(cap);
}
- var k;
- for each (k in key_event_props) { addcell(event[k]); }
- var combo = '';
+ for each (var k in key_event_props) {
+ addcell(get_field(k));
+ }
+ var charname = '';
if (event.type == 'keypress' &&
(event.charCode || event.keyCode))
{
- if (event.ctrlKey) combo += 'C-';
- if (event.metaKey || event.altKey) combo += 'M-';
- if (((event.keyCode &&
- event.charCode == 0) ||
- event.charCode == 32) &&
- event.shiftKey)
- {
- combo += 'S-';
- }
if (event.charCode) {
- if (event.charCode == 32) {
- combo += 'space';
- } else {
- combo += String.fromCharCode(event.charCode);
- }
+ if (event.charCode == 32)
+ charname = 'space';
+ else
+ charname = String.fromCharCode(event.charCode);
} else if (event.keyCode) {
- combo += keycode_to_vk_name[event.keyCode];
+ charname = keycode_to_vk_name[event.keyCode];
}
-
}
- addcell(combo);
+ addcell(charname);
table.appendChild(row);
table.ensureElementIsVisible(row);
// text output
- dump([event[k].toString().pad(' ',11)
+ dump([get_field(k).toString().pad(' ',11)
for each (k in key_event_props)].join(''));
- dumpln(combo);
+ dumpln(charname);
}
window.addEventListener("keydown", key_event_handler, true /* capture */);
window.addEventListener("keypress", key_event_handler, true /* capture */);
function onload_handler () {
- var headings = key_event_props.concat("combo");
+ var headings = key_event_props.concat("name");
var table = document.getElementById('event-table');
var head = document.createElementNS(XUL_NS,"listhead");
var coldef = document.createElementNS(XUL_NS,"listcols");
function addcol (name) {
- var t,u;
- t = document.createElementNS(XUL_NS,"listheader");
+ var t = document.createElementNS(XUL_NS,"listheader");
+ if (name in column_names)
+ name = column_names[name];
t.setAttribute("label", name);
- u = document.createElementNS(XUL_NS,"listcol");
+ var u = document.createElementNS(XUL_NS,"listcol");
u.setAttribute("flex", "1");
head.appendChild(t);
coldef.appendChild(u);
}
- var t, u;
- for each (k in headings) {
+ for each (var k in headings) {
addcol(k);
}
table.appendChild(head);
table.appendChild(coldef);
// text output
dumpln([k.pad(' ',11) for each (k in headings)].join(''));
}
-

0 comments on commit 27da1ca

Please sign in to comment.