PrOgrAMmer PrOgrAMmer - 1 year ago 191
Java Question

Key listener written in Java JNA. Prevent multiple Callback

I use the following code to listen to global key events:

import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.User32;
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.WinDef.LPARAM;
import com.sun.jna.platform.win32.WinUser;
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.WinUser.MSG;

/** Sample implementation of a low-level keyboard hook on W32. */
public class KeyHook {
private static volatile boolean quit;
private static HHOOK hhk;
private static LowLevelKeyboardProc keyboardHook;

public static void main(String[] args) {
final User32 lib = User32.INSTANCE;
HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);
keyboardHook = new LowLevelKeyboardProc() {
public LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT info) {
if (nCode >= 0) {
switch(wParam.intValue()) {
case WinUser.WM_KEYUP:
case WinUser.WM_KEYDOWN:
case WinUser.WM_SYSKEYUP:
System.err.println("in callback, key=" + info.vkCode);
if (info.vkCode == 81) {
quit = true;

Pointer ptr = info.getPointer();
long peer = Pointer.nativeValue(ptr);
return lib.CallNextHookEx(hhk, nCode, wParam, new LPARAM(peer));
hhk = lib.SetWindowsHookEx(WinUser.WH_KEYBOARD_LL, keyboardHook, hMod, 0);
System.out.println("Keyboard hook installed, type anywhere, 'q' to quit");
new Thread() {
public void run() {
while (!quit) {
try { Thread.sleep(10); } catch(Exception e) { }
System.err.println("unhook and exit");

// This bit never returns from GetMessage
int result;
MSG msg = new MSG();
while ((result = lib.GetMessage(msg, null, 0, 0)) != 0) {
if (result == -1) {
System.err.println("error in get message");
else {
System.err.println("got message");

i am wondering why the callback key called twice time if we press it one time and how we can prevent it?is it possible.This is an example of a single keypress.

in callback, key= 70
in callback, key= 70

I am not sure if i had the permission to ask another question in this topic but the question is to close with the first one and i don't want to create an another topic. so Why we got keycode=70 instead of VC_F = 0x0021. is any way to got only the VC_F code

VC_F = 0x0021;

Answer Source

The program are listening for several messages: WM_KEYUP and WM_KEYDOWN

You should listen only for WM_KEYDOWN.

Related question: Keyboard Input & the Win32 message loop

MSDN info:

If you need a more complicated behavior then you should store some info and see for successive messages to be received.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download