Add long and short hold for keybinds
This commit is contained in:
@@ -10,17 +10,40 @@
|
|||||||
|
|
||||||
let lastKeyBindUse = 0;
|
let lastKeyBindUse = 0;
|
||||||
let keyBindDelayTime = 500;
|
let keyBindDelayTime = 500;
|
||||||
|
let keyBindShortHoldDuration = 500;
|
||||||
|
let keyBindLongHoldDuration = 1500;
|
||||||
|
|
||||||
// -------------------------------------------------------------------------
|
// -------------------------------------------------------------------------
|
||||||
|
|
||||||
function bindAccountKey(key, keyState) {
|
function bindAccountKey(key, keyState) {
|
||||||
bindKey(toInteger(key), keyState, function(event) {
|
if(keyState == AG_KEYSTATE_UP) {
|
||||||
if(hasKeyBindDelayElapsed()) {
|
bindKey(toInteger(key), KEYSTATE_UP, function(event) {
|
||||||
lastKeyBindUse = sdl.ticks;
|
if(hasKeyBindDelayElapsed()) {
|
||||||
triggerNetworkEvent("ag.useKeyBind", key);
|
lastKeyBindUse = sdl.ticks;
|
||||||
}
|
triggerNetworkEvent("ag.useKeyBind", key);
|
||||||
});
|
}
|
||||||
return false;
|
});
|
||||||
|
} else if(keyState == AG_KEYSTATE_DOWN) {
|
||||||
|
bindKey(toInteger(key), KEYSTATE_DOWN, function(event) {
|
||||||
|
if(hasKeyBindDelayElapsed()) {
|
||||||
|
lastKeyBindUse = sdl.ticks;
|
||||||
|
triggerNetworkEvent("ag.useKeyBind", key);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else if(keyState == AG_KEYSTATE_HOLDSHORT || keyState == AG_KEYSTATE_HOLDLONG) {
|
||||||
|
bindKey(toInteger(key), KEYSTATE_DOWN, function(event) {
|
||||||
|
if(hasKeyBindDelayElapsed()) {
|
||||||
|
lastKeyBindUse = sdl.ticks;
|
||||||
|
keyBindHoldStart = sdl.ticks;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
bindKey(toInteger(key), KEYSTATE_UP, function(event) {
|
||||||
|
if(hasKeyBindHoldElapsed(keyState)) {
|
||||||
|
keyBindHoldStart = 0;
|
||||||
|
triggerNetworkEvent("ag.useKeyBind", key);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
addNetworkHandler("ag.addKeyBind", bindAccountKey);
|
addNetworkHandler("ag.addKeyBind", bindAccountKey);
|
||||||
|
|
||||||
@@ -42,4 +65,19 @@ function hasKeyBindDelayElapsed() {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
function hasKeyBindHoldElapsed(keyState) {
|
||||||
|
let holdDuration = AG_KEYSTATE_HOLDSHORT
|
||||||
|
if(keyState == AG_KEYSTATE_HOLDLONG) {
|
||||||
|
holdDuration = keyBindLongHoldDuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(sdl.ticks-keyBindHoldStart >= holdDuration) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// -------------------------------------------------------------------------
|
// -------------------------------------------------------------------------
|
||||||
Reference in New Issue
Block a user