mirror of
https://github.com/moebooru/moebooru
synced 2025-08-22 01:47:48 +00:00
Decaf on_key (convert)
This commit is contained in:
parent
8ba125a1f9
commit
caa784c724
@ -1,40 +1,59 @@
|
|||||||
keysDown = new Map
|
var keysDown;
|
||||||
# Many browsers eat keyup events if focus is lost while the button
|
|
||||||
# is pressed.
|
|
||||||
document.addEventListener 'blur', ->
|
|
||||||
keysDown.clear()
|
|
||||||
|
|
||||||
export onKey = (key, options, press, release) ->
|
keysDown = new Map();
|
||||||
options ?= {}
|
|
||||||
element = options.Element ? document
|
|
||||||
|
|
||||||
element.addEventListener 'keyup', (e) ->
|
// Many browsers eat keyup events if focus is lost while the button
|
||||||
if e.keyCode != key
|
// is pressed.
|
||||||
return
|
document.addEventListener('blur', function() {
|
||||||
keysDown.set(e.keyCode, false)
|
return keysDown.clear();
|
||||||
if release
|
});
|
||||||
release e
|
|
||||||
return
|
|
||||||
|
|
||||||
element.addEventListener 'keydown', (e) ->
|
export var onKey = function(key, options, press, release) {
|
||||||
if e.keyCode != key
|
var element, ref;
|
||||||
return
|
if (options == null) {
|
||||||
if e.metaKey
|
options = {};
|
||||||
return
|
}
|
||||||
if e.shiftKey != !!options.shiftKey
|
element = (ref = options.Element) != null ? ref : document;
|
||||||
return
|
element.addEventListener('keyup', function(e) {
|
||||||
if e.altKey != !!options.altKey
|
if (e.keyCode !== key) {
|
||||||
return
|
return;
|
||||||
if e.ctrlKey != !!options.ctrlKey
|
}
|
||||||
return
|
keysDown.set(e.keyCode, false);
|
||||||
if !options.allowRepeat && keysDown.get(e.keyCode) == true
|
if (release) {
|
||||||
return
|
release(e);
|
||||||
keysDown.set(e.keyCode, true)
|
}
|
||||||
target = e.target
|
});
|
||||||
if !options.AllowTextAreaFields && target.tagName == 'TEXTAREA'
|
return element.addEventListener('keydown', function(e) {
|
||||||
return
|
var target;
|
||||||
if !options.AllowInputFields && target.tagName == 'INPUT'
|
if (e.keyCode !== key) {
|
||||||
return
|
return;
|
||||||
if press? && !press(e)
|
}
|
||||||
return
|
if (e.metaKey) {
|
||||||
e.preventDefault()
|
return;
|
||||||
|
}
|
||||||
|
if (e.shiftKey !== !!options.shiftKey) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (e.altKey !== !!options.altKey) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (e.ctrlKey !== !!options.ctrlKey) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!options.allowRepeat && keysDown.get(e.keyCode) === true) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
keysDown.set(e.keyCode, true);
|
||||||
|
target = e.target;
|
||||||
|
if (!options.AllowTextAreaFields && target.tagName === 'TEXTAREA') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!options.AllowInputFields && target.tagName === 'INPUT') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if ((press != null) && !press(e)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return e.preventDefault();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user