Skip to content

Instantly share code, notes, and snippets.

@ohAitch
Forked from anonymous/addTouch.js
Last active December 26, 2016 01:25
Show Gist options
  • Save ohAitch/7539947 to your computer and use it in GitHub Desktop.
Save ohAitch/7539947 to your computer and use it in GitHub Desktop.
$.getScript("https://rawgit.com/gilmoreorless/jquery-nearest/master/src/jquery.nearest.min.js");
$("#stdKeyboard").hide()
$("#stenoKeyboard").css({
width: "95%",
height: "50%",
left: "1%"
});
function sendKey(keyDiv, dir) {
var e = $.Event(dir);
e.which = $(keyDiv).attr("class").replace(/^\D*code(\d+).*$/, "$1");
$(document).trigger(e);
}
var oldKeys = $();
function updateKeys(touches, r) {
var newKeys = $()
$.map(touches, function (t) {
newKeys = newKeys.add($(".stenoKey").touching({
x: t.pageX - r,
y: t.pageY - r,
w: 2 * r,
h: 2 * r
}))
});
console.log(newKeys);
newKeys.not(oldKeys).each(function () {
sendKey(this, "keydown")
});
oldKeys.not(newKeys).each(function () {
sendKey(this, "keyup")
});
oldKeys = newKeys;
return newKeys.length;
}
$("#stenoKeyboard").bind("touchstart touchmove touchend", function (e) {
updateKeys(e.originalEvent.touches, ($(this).width() / 100));
e.preventDefault();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment