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
|
||||
# Many browsers eat keyup events if focus is lost while the button
|
||||
# is pressed.
|
||||
document.addEventListener 'blur', ->
|
||||
keysDown.clear()
|
||||
var keysDown;
|
||||
|
||||
export onKey = (key, options, press, release) ->
|
||||
options ?= {}
|
||||
element = options.Element ? document
|
||||
keysDown = new Map();
|
||||
|
||||
element.addEventListener 'keyup', (e) ->
|
||||
if e.keyCode != key
|
||||
return
|
||||
keysDown.set(e.keyCode, false)
|
||||
if release
|
||||
release e
|
||||
return
|
||||
// Many browsers eat keyup events if focus is lost while the button
|
||||
// is pressed.
|
||||
document.addEventListener('blur', function() {
|
||||
return keysDown.clear();
|
||||
});
|
||||
|
||||
element.addEventListener 'keydown', (e) ->
|
||||
if e.keyCode != key
|
||||
return
|
||||
if e.metaKey
|
||||
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? && !press(e)
|
||||
return
|
||||
e.preventDefault()
|
||||
export var onKey = function(key, options, press, release) {
|
||||
var element, ref;
|
||||
if (options == null) {
|
||||
options = {};
|
||||
}
|
||||
element = (ref = options.Element) != null ? ref : document;
|
||||
element.addEventListener('keyup', function(e) {
|
||||
if (e.keyCode !== key) {
|
||||
return;
|
||||
}
|
||||
keysDown.set(e.keyCode, false);
|
||||
if (release) {
|
||||
release(e);
|
||||
}
|
||||
});
|
||||
return element.addEventListener('keydown', function(e) {
|
||||
var target;
|
||||
if (e.keyCode !== key) {
|
||||
return;
|
||||
}
|
||||
if (e.metaKey) {
|
||||
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