Using jna to use key events

In one of the applications that I write, I need to consume certain key events so that other applications do not process them.

In my code, I am doing com.sun.jna.platform.win32.WinUser.LowLevelKeyboardProc like this:

import com.sun.jna.Native; import com.sun.jna.platform.win32.Kernel32; import com.sun.jna.platform.win32.WinDef.HMODULE; import com.sun.jna.platform.win32.WinDef.LRESULT; import com.sun.jna.platform.win32.WinDef.WPARAM; import com.sun.jna.platform.win32.WinUser.HHOOK; import com.sun.jna.platform.win32.WinUser.KBDLLHOOKSTRUCT; import com.sun.jna.platform.win32.WinUser.LowLevelKeyboardProc; import com.sun.jna.platform.win32.User32; import com.sun.jna.platform.win32.WinUser; public class KeyHook implements Runnable{ private static volatile boolean quit = false; private static HHOOK hhk; private static LowLevelKeyboardProc keyboardHook; private Main main; User32 lib; HMODULE hMod; public boolean isHooked = false; public KeyHook(final Main main) { this.main = main; lib = User32.INSTANCE; hMod = Kernel32.INSTANCE.GetModuleHandle(null); Native.setProtected(true); } @Override public void run() { keyboardHook = new LowLevelKeyboardProc() { public LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT info) { if (nCode >= 0 && main.getPane().getTabCount() > 0) { switch (wParam.intValue()) { case WinUser.WM_KEYUP: if(info.vkCode == main.getListenMouse()){ main.listen(); return new LRESULT(1); } else if(info.vkCode == main.getStopListenMouse()){ main.stopListening(); return new LRESULT(1); } else if(info.vkCode == main.getStart()){ main.start(); return new LRESULT(1); } else if(info.vkCode == main.getPause()){ main.pause(); return new LRESULT(1); } else if(info.vkCode == main.getStop()){ main.stopRunning(); return new LRESULT(1); } else if(info.vkCode == 0x7B){ main.nextTab(); return new LRESULT(1); } break; case WinUser.WM_KEYDOWN: break; case WinUser.WM_SYSKEYUP: break; case WinUser.WM_SYSKEYDOWN: quit = true; break; } } return lib.CallNextHookEx(hhk, nCode, wParam, info.getPointer()); //return new LRESULT(1); } }; hhk = lib.SetWindowsHookEx(WinUser.WH_KEYBOARD_LL, keyboardHook, hMod, 0); } } 

When I return a new LRESULT (1) at the end of my proc (code with comments at the end), all keyboard events are consumed. However, when I replace it with

 return lib.CallNextHookEx(hhk, nCode, wParam, info.getPointer()); 

as it should be, and just try to use the main keyboard events that I want to use, it does not consume any keyboard events. Does anyone know why he will not allow me to consume the events I want, or know how to fix this?

+1
source share
1 answer

To ensure that the key is "consumed", you need to make sure that you do not call the next hook (that is, return LRESULT (1)) for all event options for this key, that is WM_KEYUP, WM_KEYDOWN, and possibly WM_CHAR.

Some applications may look for key events, others for the down keys, and others just for the generated character output, so you must consume all the events associated with a given keystroke to make the specified keystroke "disappear".

+1
source

Source: https://habr.com/ru/post/1437745/


All Articles