Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keydown event only triggered by modifier key with Electron 12 #332

Open
jopemachine opened this issue Jun 3, 2021 · 7 comments
Open

Keydown event only triggered by modifier key with Electron 12 #332

jopemachine opened this issue Jun 3, 2021 · 7 comments
Assignees
Labels
bug An identified bug, though not necessarily being currently investigated electron Electron-related issues, bugs or questions investigating Currently trying to investigate and reproduce issue
Milestone

Comments

@jopemachine
Copy link

jopemachine commented Jun 3, 2021

Expected Behavior

Keydown event is triggered by all type of key down

Current Behavior

Only detect modifier key (cmd, ctrl, alt...).

Alphabet, number keys are not detected.

Possible Solution

I have no idea why this is happening.

Steps to Reproduce (for bugs)

  1. Use iohook in Renderer process
  2. It is working fine, but only detect modifier key (cmd, ctrl, alt...). alphabet, number key is not detected.
  3. On other platforms, behavior is the same.

Context

ioHook.on('keydown', (e) => {
  console.log(e);
});

Your Environment

@jopemachine
Copy link
Author

jopemachine commented Jun 11, 2021

I don't know why this happens, the bug doesn't happen on production mode.

@ash0x0
Copy link
Collaborator

ash0x0 commented Jun 11, 2021

@jopemachine it'll be some time before I get around to electron 12 and 13 but do provide more information if you can, such as if this behavior is the same on other OSes and what you're using to build your code for production.

@ash0x0 ash0x0 self-assigned this Jun 11, 2021
@ash0x0 ash0x0 added bug An identified bug, though not necessarily being currently investigated electron Electron-related issues, bugs or questions awaiting feedback Waiting on an answer or information from issue or PR author to proceed labels Jun 11, 2021
@jopemachine
Copy link
Author

@jopemachine it'll be some time before I get around to electron 12 and 13 but do provide more information if you can, such as if this behavior is the same on other OSes and what you're using to build your code for production.

Now I've made the project public.

here is my code using iohook.

if this behavior is the same on other OSes

Yes, this behavior is same on other OSes.

@ash0x0
Copy link
Collaborator

ash0x0 commented Jun 15, 2021

@jopemachine thank you. It seems that for this particular use you need keydown event. If I'm mistaken about that then you might want to try keypress event instead.
It will probably be around a week at least before I can fix this because there are other issues affecting this that need to be fixed first.

@ash0x0 ash0x0 removed the awaiting feedback Waiting on an answer or information from issue or PR author to proceed label Jun 15, 2021
@ash0x0 ash0x0 added this to the Todo milestone Jun 15, 2021
@jopemachine
Copy link
Author

jopemachine commented Jun 16, 2021

@jopemachine thank you. It seems that for this particular use you need keydown event. If I'm mistaken about that then you might want to try keypress event instead.
It will probably be around a week at least before I can fix this because there are other issues affecting this that need to be fixed first.

I appreciate your hard work.

I tried to replaceing the event with the keypress event, but unfortunately keypress event was not triggered.

Here is my test code.

  useEffect(() => {
    ioHook.on('keypress', (e: IOHookKeyEvent) => {
      // not works on development mode, production mode both.
      console.log('triggered!');

      if (cpyKeyPressed(e)) {
        setTimeout(() => {
          const copiedText = clipboard.readText();
          if (copiedText !== '') {
            ipcRenderer.send(IPCRendererEnum.dispatchAction, {
              destWindow: 'clipboardHistoryWindow',
              actionType: actionTypes.PUSH_CLIPBOARD_STORE,
              args: JSON.stringify({
                text: clipboard.readText(),
                date: new Date().getTime(),
              }),
            });
          }
        }, 25);
      }

      if (isShiftKey(e)) {
        handleDoubleKeyModifier('shift');
      } else if (isAltKey(e)) {
        handleDoubleKeyModifier('alt');
      } else if (isCtrlKey(e)) {
        handleDoubleKeyModifier('ctrl');
      } else if (isMetaKey(e)) {
        handleDoubleKeyModifier('cmd');
      }
    });

    ioHook.start();

    return () => {
      ioHook.removeAllListeners();
      ioHook.unload();
    };
  }, []);

@ash0x0 ash0x0 added the investigating Currently trying to investigate and reproduce issue label Jun 28, 2021
@bugii
Copy link

bugii commented Sep 8, 2021

I think I also once encountered that issue on macOS. For me, it was related to the fact that the terminal application used to start my application no longer had the required accessibility privileges (for keyboard input). After enabling them again in the system settings, everything worked again.

@jopemachine
Copy link
Author

jopemachine commented Sep 9, 2021

I think I also once encountered that issue on macOS. For me, it was related to the fact that the terminal application used to start my application no longer had the required accessibility privileges (for keyboard input). After enabling them again in the system settings, everything worked again.

I appreciate for your comment :)

It works well on dev mode now after activating vscode's Input Monitoring permission on mac.

I think maybe there is another similar kind of permission setting in other OSs too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An identified bug, though not necessarily being currently investigated electron Electron-related issues, bugs or questions investigating Currently trying to investigate and reproduce issue
Projects
None yet
Development

No branches or pull requests

3 participants