如何在 Windows 10 上禁用 electron 应用程序中的键盘快捷键 Alt + Tab?

3

我正在使用桌面应用程序,在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()
})

2
你能解释一下为什么需要防止用户切换到另一个应用程序吗?我所能想到的唯一用例涉及恶意软件的创建。 - Dragonthoughts
2
因为我正在开发在线考试系统,为了防止学生作弊,所以我必须这样做。 - Sopha Sum
1
好的,这是一个很好的理由,谢谢。 - Dragonthoughts
2
也许另一种尝试是检测窗口何时失去焦点?这样你就可以告诉用户不要切换窗口,否则他将无法通过测试。如果他仍然这样做,你将会检测到并让他失败。 - Tom Doodler
1
@Tom Doodler,为什么不使用'browser-window-blur'发布一个答案呢?;) - pergy
显示剩余6条评论
1个回答

3

您可以使用globalShortcut模块,即使应用程序没有键盘焦点,也可以检测键盘事件。

const { app, globalShortcut } = require('electron')

app.on('ready', () => {
  globalShortcut.register('alt+tab', () => {

     return false
  })
})

希望这有所帮助。

你好,@siddharth shah 兄弟,它说 window 没有定义。 - Sopha Sum
我发现了一个非常有用的包,可以在electronjs中管理键盘事件。这将解决你的问题。https://www.npmjs.com/package/electron-localshortcut - siddharth shah
我已经检查了那个链接并测试了那段代码,但它仍然无法工作。 - Sopha Sum
@SophaSum 你需要在 main.js 文件中使用它,并为每个窗口注册。 - siddharth shah
感谢您的帮助,我已经使用过这段代码了,但它只能在Linux操作系统上正常工作,而在Windows操作系统上Alt键盘仍然无法正常工作。 - Sopha Sum
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接