【发布时间】:2018-12-15 23:30:53
【问题描述】:
我正在使用桌面应用程序,当我在 Ubuntu 服务器上工作时,我可以阻止键盘快捷键:Alt+Tab 很好,但是当我移动到在 Windows 操作系统上工作并尝试阻止 Alt+Tab,它不起作用。 Alt 键盘 造成的问题最多,当我尝试阻止它时,它在 Windows 10 上严重不起作用 这是我正在使用的代码:
var shortcutsToCapture = ['Ctrl+Alt+Delete', 'Alt+F4','CommandOrControl+A','Super+Alt+Tab','CommandOrControl+Shift+I', 'CommandOrControl+R']
// this should be placed at top of main.js to handle setup events quickly
if (handleSquirrelEvent(app)) {
// squirrel event handled and app will exit in 1000ms, so don't do anything else
return;
}
app.on('ready', function () {
captureShortcuts(shortcutsToCapture)
})
function captureShortcuts(shortcuts) {
shortcuts.forEach(function (shortcut) {
registerShortcutCapturing(shortcut)
})
}
function registerShortcutCapturing(shortcut) {
var result = globalShortcut.register(shortcut, function () {
console.log('<' + shortcut + '> captured!')
})
if (!result) {
console.log('<' + shortcut + '> registration failed!')
}
}
app.on('will-quit', () => {
// Unregister a shortcut.
globalShortcut.unregister('CommandOrControl+X')
// Unregister all shortcuts.
globalShortcut.unregisterAll()
})
【问题讨论】:
-
您能解释一下为什么需要阻止用户切换到另一个应用程序吗?我能想到的唯一用例涉及创建恶意软件。
-
@Dragonthoughts 因为我正在开发在线考试系统,所以为了防止学生作弊,我必须这样做。
-
好的,这是一个很好的理由,谢谢。
-
也许另一种尝试是检测窗口何时失去焦点?通过这种方式,您可以告诉用户不要切换窗口,否则他将无法通过测试。如果他仍然这样做,你会发现它并让他失败。
-
@Tom Doodler 为什么你不发布答案然后使用
'browser-window-blur'? ;)
标签: javascript node.js electron