gpt4 book ai didi

javascript - 无法覆盖键盘 ALT 修饰符

转载 作者:行者123 更新时间:2023-11-29 15:51:27 25 4
gpt4 key购买 nike

我正在编写一个 Javascript 类来处理击键和击键组合。例如,以下将为 SHIFT 键添加回调。

MYAPP.Keyboard.instance().observe(
MYAPP.Keyboard.type.KEYDOWN,
MYAPP.Keyboard.key.SHIFT,
function() {
$$('body').first().addClassName('keyboardHintShow');
}
);

SHIFT+F 的示例是:

MYAPP.Keyboard.instance().observe(
MYAPP.Keyboard.type.KEYUP,
[MYAPP.Keyboard.key.F, MYAPP.Keyboard.key.SHIFT],
function() {
MYAPP.Broadcast.instance().signal('file');
}
);

到目前为止,这在 Firefox 中运行良好,但当我尝试使用 ALT 作为修饰符时却不行。出现两个问题。 1) 事件传播没有停止,因此弹出 Firefox 的文件菜单。 2)释放ALTF后,只有F的onKeyUp触发,所以ALT的状态是错误的.是什么让 ALTSHIFT 如此不同?

完整代码如下:

MYAPP.Keyboard = Class.create({

/*******************************************************************************
PUBLIC PUBLIC PUBLIC PUBLIC PUBLIC PUBLIC PUBLIC PUBLIC PUBLIC PUBLIC PUBLIC PUB
*******************************************************************************/

/**
* @return pointer
*/
initialize: function() {
this.downKeys = new Hash();
this.observers = new Hash();

document.observe(
'keydown',
this.onKeyDown.bind(this)
);

document.observe(
'keyup',
this.onKeyUp.bind(this)
);
},

/**
* @param MYAPP.Keyboard.type type
* @param MYAPP.Keyboard.key | array of MYAPP.Keyboard.key keys
* @param void function() callback
* @return void
*/
observe: function(type, keys, callback) {
var main;
var modifiers;

if (typeof(keys) === 'number') {
main = keys;
modifiers = [];
} else {
main = keys.first();
modifiers = keys.slice(1);
}

if (this.observers.get(type) === undefined) {
this.observers.set(type, new Hash());
}

this.observers.get(type).set(
main, {
modifiers: modifiers,
callback: callback
}
);
},

/**
* @param MYAPP.Keyboard.type type
* @param MYAPP.Keyboard.key
* @return void
*/
stopObserving: function(type, key) {
this.observers.get(type).unset(key);
},

/*******************************************************************************
PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE
*******************************************************************************/

/**
* @param Event event
* @return void
*/
onKeyDown: function(event) {
if (this.downKeys.get(event.keyCode) === true) {
return;
}

this.downKeys.set(event.keyCode, true);

var downObservers = this.observers.get(MYAPP.Keyboard.type.KEYDOWN);
if (downObservers !== undefined) {
this.runCallback(downObservers, event);
}
return false;
},

/**
* @param Event event
* @return void
*/
onKeyUp: function(event) {
this.downKeys.set(event.keyCode, false);

var downObservers = this.observers.get(MYAPP.Keyboard.type.KEYUP);
if (downObservers !== undefined) {
this.runCallback(downObservers, event);
}
return false;
},

/**
* @param Hash observers
* @param Event event
* @return void
*/
runCallback: function(observers, event) {
var overrideBrowser = false;
var order = observers.get(event.keyCode);
if (order !== undefined) {
if (order.modifiers.size() === 0) {
order.callback();
Event.stop(event);
} else {
for (var i = 0; i < order.modifiers.size(); i++) {
var modifierStatus = this.downKeys.get(order.modifiers[i]);
if (modifierStatus === undefined || modifierStatus === false) {
return;
}
order.callback();
Event.stop(event);
}
}
}
}
});

/*******************************************************************************
STATIC STATIC STATIC STATIC STATIC STATIC STATIC STATIC STATIC STATIC STATIC STA
*******************************************************************************/

/**
* @return Keyboard instance
*/
MYAPP.Keyboard.instance = function() {
if (typeof(MYAPP.keyboard) === 'undefined') {
MYAPP.keyboard = new MYAPP.Keyboard();
}
return MYAPP.keyboard;
};

/**
* Event type
*/
MYAPP.Keyboard.type = {
KEYUP: 0,
KEYDOWN: 1
};

/**
* Keycodes for various keys.
*/
MYAPP.Keyboard.key = {
SHIFT: 16,
CTRL: 17,
ALT: 18,
A: 65,
B: 66,
C: 67,
D: 68,
E: 69,
F: 70,
G: 71,
H: 72,
I: 73,
J: 74,
K: 75,
L: 76,
M: 77,
N: 78,
O: 79,
P: 80,
Q: 81,
R: 82,
S: 83,
T: 84,
U: 85,
V: 86,
W: 87,
X: 88,
Y: 89,
Z: 90
};

最佳答案

This可能会有所帮助作为一个例子。在页面底部,作者声称他的脚本覆盖了所有修饰符 - Ctrl、Alt、Shift、Meta。希望这能以某种方式有所帮助。

关于javascript - 无法覆盖键盘 ALT 修饰符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5149401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com