Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

View RSS Feed

helloworld922

Minitip #2: Determining if a key is being pressed [WINDOWS SPECIFIC]

Rate this Entry
Sometimes it's valuable to determine if a certain key is being pressed. This is especially valuable if you have multiple Swing components and you don't want to mess with keeping track of boolean flags across different objects, but is absolutely necessary if you're trying to get keypresses which are not received by any of your Swing components.

In the forums, I've recommended writing JNI code which would allow the Java code to poll the hardware for key states, but never actually got around to actually writing the code or posting a link. So as a starting point, I'm going to provide the code for Windows systems only.

The Java code:

package keyboard;
 
public class QueryKeyboard
{
	static
	{
		System.loadLibrary("QueryKeyboard");
	}
 
	/**
	 * Determines if the current key is being pressed.
	 * 
	 * @param keycode
	 * @return true if the key is being pressed
	 */
	public static native boolean isKeyPressed(int keycode);
 
	/**
	 * Left mouse button
	 */
	final static int VK_LBUTTON = 0x01;
 
	/**
	 * Right mouse button
	 */
	final static int VK_RBUTTON = 0x02;
 
	/**
	 * Control-break processing
	 */
	final static int VK_CANCEL = 0x03;
 
	/**
	 * Middle mouse button (three-button mouse)
	 */
	final static int VK_MBUTTON = 0x04;
 
	/**
	 * X1 mouse button
	 */
	final static int VK_XBUTTON1 = 0x05;
 
	/**
	 * X2 mouse button
	 */
	final static int VK_XBUTTON2 = 0x06;
 
	/**
	 * BACKSPACE key
	 */
	final static int VK_BACK = 0x08;
 
	/**
	 * TAB key
	 */
	final static int VK_TAB = 0x09;
 
	/**
	 * CLEAR key
	 */
	final static int VK_CLEAR = 0x0C;
 
	/**
	 * ENTER key
	 */
	final static int VK_RETURN = 0x0D;
 
	/**
	 * SHIFT key
	 */
	final static int VK_SHIFT = 0x10;
 
	/**
	 * CTRL key
	 */
	final static int VK_CONTROL = 0x11;
 
	/**
	 * ALT key
	 */
	final static int VK_MENU = 0x12;
 
	/**
	 * PAUSE key
	 */
	final static int VK_PAUSE = 0x13;
 
	/**
	 * CAPS LOCK key
	 */
	final static int VK_CAPITAL = 0x14;
 
	/**
	 * IME Kana mode
	 */
	final static int VK_KANA = 0x15;
 
	/**
	 * IME Hanguel mode (maintained for compatibility; use VK_HANGUL)
	 */
	final static int VK_HANGUEL = 0x15;
 
	/**
	 * IME Hangul mode
	 */
	final static int VK_HANGUL = 0x15;
 
	/**
	 * IME Junja mode
	 */
	final static int VK_JUNJA = 0x17;
 
	/**
	 * IME final mode
	 */
	final static int VK_FINAL = 0x18;
 
	/**
	 * IME Hanja mode
	 */
	final static int VK_HANJA = 0x19;
 
	/**
	 * IME Kanji mode
	 */
	final static int VK_KANJI = 0x19;
 
	/**
	 * ESC key
	 */
	final static int VK_ESCAPE = 0x1B;
 
	/**
	 * IME convert
	 */
	final static int VK_CONVERT = 0x1C;
 
	/**
	 * IME nonconvert
	 */
	final static int VK_NONCONVERT = 0x1D;
 
	/**
	 * IME accept
	 */
	final static int VK_ACCEPT = 0x1E;
 
	/**
	 * IME mode change request
	 */
	final static int VK_MODECHANGE = 0x1F;
 
	/**
	 * SPACEBAR
	 */
	final static int VK_SPACE = 0x20;
 
	/**
	 * PAGE UP key
	 */
	final static int VK_PRIOR = 0x21;
 
	/**
	 * PAGE DOWN key
	 */
	final static int VK_NEXT = 0x22;
 
	/**
	 * END key
	 */
	final static int VK_END = 0x23;
 
	/**
	 * HOME key
	 */
	final static int VK_HOME = 0x24;
 
	/**
	 * LEFT ARROW key
	 */
	final static int VK_LEFT = 0x25;
 
	/**
	 * UP ARROW key
	 */
	final static int VK_UP = 0x26;
 
	/**
	 * RIGHT ARROW key
	 */
	final static int VK_RIGHT = 0x27;
 
	/**
	 * DOWN ARROW key
	 */
	final static int VK_DOWN = 0x28;
 
	/**
	 * SELECT key
	 */
	final static int VK_SELECT = 0x29;
 
	/**
	 * PRINT key
	 */
	final static int VK_PRINT = 0x2A;
 
	/**
	 * EXECUTE key
	 */
	final static int VK_EXECUTE = 0x2B;
 
	/**
	 * PRINT SCREEN key
	 */
	final static int VK_SNAPSHOT = 0x2C;
 
	/**
	 * INS key
	 */
	final static int VK_INSERT = 0x2D;
 
	/**
	 * DEL key
	 */
	final static int VK_DELETE = 0x2E;
 
	/**
	 * HELP key
	 */
	final static int VK_HELP = 0x2F;
 
	/**
	 * 0 key
	 */
	final static int VK_0 = 0x30;
 
	/**
	 * 1 key
	 */
	final static int VK_1 = 0x31;
 
	/**
	 * 2 key
	 */
	final static int VK_2 = 0x32;
 
	/**
	 * 3 key
	 */
	final static int VK_3 = 0x33;
 
	/**
	 * 4 key
	 */
	final static int VK_4 = 0x34;
 
	/**
	 * 5 key
	 */
	final static int VK_5 = 0x35;
 
	/**
	 * 6 key
	 */
	final static int VK_6 = 0x36;
 
	/**
	 * 7 key
	 */
	final static int VK_7 = 0x37;
 
	/**
	 * 8 key
	 */
	final static int VK_8 = 0x38;
 
	/**
	 * 9 key
	 */
	final static int VK_9 = 0x39;
 
	/**
	 * A key
	 */
	final static int VK_A = 0x41;
 
	/**
	 * B key
	 */
	final static int VK_B = 0x42;
 
	/**
	 * C key
	 */
	final static int VK_C = 0x43;
 
	/**
	 * D key
	 */
	final static int VK_D = 0x44;
 
	/**
	 * E key
	 */
	final static int VK_E = 0x45;
 
	/**
	 * F key
	 */
	final static int VK_F = 0x46;
 
	/**
	 * G key
	 */
	final static int VK_G = 0x47;
 
	/**
	 * H key
	 */
	final static int VK_H = 0x48;
 
	/**
	 * I key
	 */
	final static int VK_I = 0x49;
 
	/**
	 * J key
	 */
	final static int VK_J = 0x4A;
 
	/**
	 * K key
	 */
	final static int VK_K = 0x4B;
 
	/**
	 * L key
	 */
	final static int VK_L = 0x4C;
 
	/**
	 * M key
	 */
	final static int VK_M = 0x4D;
 
	/**
	 * N key
	 */
	final static int VK_N = 0x4E;
 
	/**
	 * O key
	 */
	final static int VK_O = 0x4F;
 
	/**
	 * P key
	 */
	final static int VK_P = 0x50;
 
	/**
	 * Q key
	 */
	final static int VK_Q = 0x51;
 
	/**
	 * R key
	 */
	final static int VK_R = 0x52;
 
	/**
	 * S key
	 */
	final static int VK_S = 0x53;
 
	/**
	 * T key
	 */
	final static int VK_T = 0x54;
 
	/**
	 * U key
	 */
	final static int VK_U = 0x55;
 
	/**
	 * V key
	 */
	final static int VK_V = 0x56;
 
	/**
	 * W key
	 */
	final static int VK_W = 0x57;
 
	/**
	 * X key
	 */
	final static int VK_X = 0x58;
 
	/**
	 * Y key
	 */
	final static int VK_Y = 0x59;
 
	/**
	 * Z key
	 */
	final static int VK_Z = 0x5A;
 
	/**
	 * Left Windows key (Natural keyboard)
	 */
	final static int VK_LWIN = 0x5B;
 
	/**
	 * Right Windows key (Natural keyboard)
	 */
	final static int VK_RWIN = 0x5C;
 
	/**
	 * Applications key (Natural keyboard)
	 */
	final static int VK_APPS = 0x5D;
 
	/**
	 * Computer Sleep key
	 */
	final static int VK_SLEEP = 0x5F;
 
	/**
	 * Numeric keypad 0 key
	 */
	final static int VK_NUMPAD0 = 0x60;
 
	/**
	 * Numeric keypad 1 key
	 */
	final static int VK_NUMPAD1 = 0x61;
 
	/**
	 * Numeric keypad 2 key
	 */
	final static int VK_NUMPAD2 = 0x62;
 
	/**
	 * Numeric keypad 3 key
	 */
	final static int VK_NUMPAD3 = 0x63;
 
	/**
	 * Numeric keypad 4 key
	 */
	final static int VK_NUMPAD4 = 0x64;
 
	/**
	 * Numeric keypad 5 key
	 */
	final static int VK_NUMPAD5 = 0x65;
 
	/**
	 * Numeric keypad 6 key
	 */
	final static int VK_NUMPAD6 = 0x66;
 
	/**
	 * Numeric keypad 7 key
	 */
	final static int VK_NUMPAD7 = 0x67;
 
	/**
	 * Numeric keypad 8 key
	 */
	final static int VK_NUMPAD8 = 0x68;
 
	/**
	 * Numeric keypad 9 key
	 */
	final static int VK_NUMPAD9 = 0x69;
 
	/**
	 * Multiply key
	 */
	final static int VK_MULTIPLY = 0x6A;
 
	/**
	 * Add key
	 */
	final static int VK_ADD = 0x6B;
 
	/**
	 * Separator key
	 */
	final static int VK_SEPARATOR = 0x6C;
 
	/**
	 * Subtract key
	 */
	final static int VK_SUBTRACT = 0x6D;
 
	/**
	 * Decimal key
	 */
	final static int VK_DECIMAL = 0x6E;
 
	/**
	 * Divide key
	 */
	final static int VK_DIVIDE = 0x6F;
 
	/**
	 * F1 key
	 */
	final static int VK_F1 = 0x70;
 
	/**
	 * F2 key
	 */
	final static int VK_F2 = 0x71;
 
	/**
	 * F3 key
	 */
	final static int VK_F3 = 0x72;
 
	/**
	 * F4 key
	 */
	final static int VK_F4 = 0x73;
 
	/**
	 * F5 key
	 */
	final static int VK_F5 = 0x74;
 
	/**
	 * F6 key
	 */
	final static int VK_F6 = 0x75;
 
	/**
	 * F7 key
	 */
	final static int VK_F7 = 0x76;
 
	/**
	 * F8 key
	 */
	final static int VK_F8 = 0x77;
 
	/**
	 * F9 key
	 */
	final static int VK_F9 = 0x78;
 
	/**
	 * F10 key
	 */
	final static int VK_F10 = 0x79;
 
	/**
	 * F11 key
	 */
	final static int VK_F11 = 0x7A;
 
	/**
	 * F12 key
	 */
	final static int VK_F12 = 0x7B;
 
	/**
	 * F13 key
	 */
	final static int VK_F13 = 0x7C;
 
	/**
	 * F14 key
	 */
	final static int VK_F14 = 0x7D;
 
	/**
	 * F15 key
	 */
	final static int VK_F15 = 0x7E;
 
	/**
	 * F16 key
	 */
	final static int VK_F16 = 0x7F;
 
	/**
	 * F17 key
	 */
	final static int VK_F17 = 0x80;
 
	/**
	 * F18 key
	 */
	final static int VK_F18 = 0x81;
 
	/**
	 * F19 key
	 */
	final static int VK_F19 = 0x82;
 
	/**
	 * F20 key
	 */
	final static int VK_F20 = 0x83;
 
	/**
	 * F21 key
	 */
	final static int VK_F21 = 0x84;
 
	/**
	 * F22 key
	 */
	final static int VK_F22 = 0x85;
 
	/**
	 * F23 key
	 */
	final static int VK_F23 = 0x86;
 
	/**
	 * F24 key
	 */
	final static int VK_F24 = 0x87;
 
	/**
	 * NUM LOCK key
	 */
	final static int VK_NUMLOCK = 0x90;
 
	/**
	 * SCROLL LOCK key
	 */
	final static int VK_SCROLL = 0x91;
 
	/**
	 * Left SHIFT key
	 */
	final static int VK_LSHIFT = 0xA0;
 
	/**
	 * Right SHIFT key
	 */
	final static int VK_RSHIFT = 0xA1;
 
	/**
	 * Left CONTROL key
	 */
	final static int VK_LCONTROL = 0xA2;
 
	/**
	 * Right CONTROL key
	 */
	final static int VK_RCONTROL = 0xA3;
 
	/**
	 * Left MENU key
	 */
	final static int VK_LMENU = 0xA4;
 
	/**
	 * Right MENU key
	 */
	final static int VK_RMENU = 0xA5;
 
	/**
	 * Browser Back key
	 */
	final static int VK_BROWSER_BACK = 0xA6;
 
	/**
	 * Browser Forward key
	 */
	final static int VK_BROWSER_FORWARD = 0xA7;
 
	/**
	 * Browser Refresh key
	 */
	final static int VK_BROWSER_REFRESH = 0xA8;
 
	/**
	 * Browser Stop key
	 */
	final static int VK_BROWSER_STOP = 0xA9;
 
	/**
	 * Browser Search key
	 */
	final static int VK_BROWSER_SEARCH = 0xAA;
 
	/**
	 * Browser Favorites key
	 */
	final static int VK_BROWSER_FAVORITES = 0xAB;
 
	/**
	 * Browser Start and Home key
	 */
	final static int VK_BROWSER_HOME = 0xAC;
 
	/**
	 * Volume Mute key
	 */
	final static int VK_VOLUME_MUTE = 0xAD;
 
	/**
	 * Volume Down key
	 */
	final static int VK_VOLUME_DOWN = 0xAE;
 
	/**
	 * Volume Up key
	 */
	final static int VK_VOLUME_UP = 0xAF;
 
	/**
	 * Next Track key
	 */
	final static int VK_MEDIA_NEXT_TRACK = 0xB0;
 
	/**
	 * Previous Track key
	 */
	final static int VK_MEDIA_PREV_TRACK = 0xB1;
 
	/**
	 * Stop Media key
	 */
	final static int VK_MEDIA_STOP = 0xB2;
 
	/**
	 * Play/Pause Media key
	 */
	final static int VK_MEDIA_PLAY_PAUSE = 0xB3;
 
	/**
	 * Start Mail key
	 */
	final static int VK_LAUNCH_MAIL = 0xB4;
 
	/**
	 * Select Media key
	 */
	final static int VK_LAUNCH_MEDIA_SELECT = 0xB5;
 
	/**
	 * Start Application 1 key
	 */
	final static int VK_LAUNCH_APP1 = 0xB6;
 
	/**
	 * Start Application 2 key
	 */
	final static int VK_LAUNCH_APP2 = 0xB7;
 
	/**
	 * Used for miscellaneous characters; it can vary by keyboard. For the US
	 * standard keyboard, the ';:' key
	 */
	final static int VK_OEM_1 = 0xBA;
 
	/**
	 * For any country/region, the '+' key
	 */
	final static int VK_OEM_PLUS = 0xBB;
 
	/**
	 * For any country/region, the ',' key
	 */
	final static int VK_OEM_COMMA = 0xBC;
 
	/**
	 * For any country/region, the '-' key
	 */
	final static int VK_OEM_MINUS = 0xBD;
 
	/**
	 * For any country/region, the '.' key
	 */
	final static int VK_OEM_PERIOD = 0xBE;
 
	/**
	 * Used for miscellaneous characters; it can vary by keyboard. For the US
	 * standard keyboard, the '/?' key
	 */
	final static int VK_OEM_2 = 0xBF;
 
	/**
	 * Used for miscellaneous characters; it can vary by keyboard. For the US
	 * standard keyboard, the '`~' key
	 */
	final static int VK_OEM_3 = 0xC0;
 
	/**
	 * Used for miscellaneous characters; it can vary by keyboard. For the US
	 * standard keyboard, the '[{' key
	 */
	final static int VK_OEM_4 = 0xDB;
 
	/**
	 * Used for miscellaneous characters; it can vary by keyboard. For the US
	 * standard keyboard, the '\|' key
	 */
	final static int VK_OEM_5 = 0xDC;
 
	/**
	 * Used for miscellaneous characters; it can vary by keyboard. For the US
	 * standard keyboard, the ']}' key
	 */
	final static int VK_OEM_6 = 0xDD;
 
	/**
	 * Used for miscellaneous characters; it can vary by keyboard. For the US
	 * standard keyboard, the 'single-quote/double-quote' key
	 */
	final static int VK_OEM_7 = 0xDE;
 
	/**
	 * Used for miscellaneous characters; it can vary by keyboard.
	 */
	final static int VK_OEM_8 = 0xDF;
 
	/**
	 * Either the angle bracket key or the backslash key on the RT 102-key
	 * keyboard
	 */
	final static int VK_OEM_102 = 0xE2;
 
	/**
	 * IME PROCESS key
	 */
	final static int VK_PROCESSKEY = 0xE5;
 
	/**
	 * Used to pass Unicode characters as if they were keystrokes. The VK_PACKET
	 * key is the low word of a 32-bit Virtual Key value used for non-keyboard
	 * input methods. For more information, see Remark in KEYBDINPUT, SendInput,
	 * WM_KEYDOWN, and WM_KEYUP
	 */
	final static int VK_PACKET = 0xE7;
 
	/**
	 * Attn key
	 */
	final static int VK_ATTN = 0xF6;
 
	/**
	 * CrSel key
	 */
	final static int VK_CRSEL = 0xF7;
 
	/**
	 * ExSel key
	 */
	final static int VK_EXSEL = 0xF8;
 
	/**
	 * Erase EOF key
	 */
	final static int VK_EREOF = 0xF9;
 
	/**
	 * Play key
	 */
	final static int VK_PLAY = 0xFA;
 
	/**
	 * Zoom key
	 */
	final static int VK_ZOOM = 0xFB;
 
	/**
	 * Reserved
	 */
	final static int VK_NONAME = 0xFC;
 
	/**
	 * PA1 key
	 */
	final static int VK_PA1 = 0xFD;
 
	/**
	 * Clear key
	 */
	final static int VK_OEM_CLEAR = 0xFE;
}

The JNI C++ code:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
#include <Windows.h>
/* Header for class keyboard_QueryKeyboard */
 
#ifndef _Included_keyboard_QueryKeyboard
#define _Included_keyboard_QueryKeyboard
#ifdef __cplusplus
extern "C" {
#endif
#undef keyboard_QueryKeyboard_VK_LBUTTON
#define keyboard_QueryKeyboard_VK_LBUTTON 1L
#undef keyboard_QueryKeyboard_VK_RBUTTON
#define keyboard_QueryKeyboard_VK_RBUTTON 2L
#undef keyboard_QueryKeyboard_VK_CANCEL
#define keyboard_QueryKeyboard_VK_CANCEL 3L
#undef keyboard_QueryKeyboard_VK_MBUTTON
#define keyboard_QueryKeyboard_VK_MBUTTON 4L
#undef keyboard_QueryKeyboard_VK_XBUTTON1
#define keyboard_QueryKeyboard_VK_XBUTTON1 5L
#undef keyboard_QueryKeyboard_VK_XBUTTON2
#define keyboard_QueryKeyboard_VK_XBUTTON2 6L
#undef keyboard_QueryKeyboard_VK_BACK
#define keyboard_QueryKeyboard_VK_BACK 8L
#undef keyboard_QueryKeyboard_VK_TAB
#define keyboard_QueryKeyboard_VK_TAB 9L
#undef keyboard_QueryKeyboard_VK_CLEAR
#define keyboard_QueryKeyboard_VK_CLEAR 12L
#undef keyboard_QueryKeyboard_VK_RETURN
#define keyboard_QueryKeyboard_VK_RETURN 13L
#undef keyboard_QueryKeyboard_VK_SHIFT
#define keyboard_QueryKeyboard_VK_SHIFT 16L
#undef keyboard_QueryKeyboard_VK_CONTROL
#define keyboard_QueryKeyboard_VK_CONTROL 17L
#undef keyboard_QueryKeyboard_VK_MENU
#define keyboard_QueryKeyboard_VK_MENU 18L
#undef keyboard_QueryKeyboard_VK_PAUSE
#define keyboard_QueryKeyboard_VK_PAUSE 19L
#undef keyboard_QueryKeyboard_VK_CAPITAL
#define keyboard_QueryKeyboard_VK_CAPITAL 20L
#undef keyboard_QueryKeyboard_VK_KANA
#define keyboard_QueryKeyboard_VK_KANA 21L
#undef keyboard_QueryKeyboard_VK_HANGUEL
#define keyboard_QueryKeyboard_VK_HANGUEL 21L
#undef keyboard_QueryKeyboard_VK_HANGUL
#define keyboard_QueryKeyboard_VK_HANGUL 21L
#undef keyboard_QueryKeyboard_VK_JUNJA
#define keyboard_QueryKeyboard_VK_JUNJA 23L
#undef keyboard_QueryKeyboard_VK_FINAL
#define keyboard_QueryKeyboard_VK_FINAL 24L
#undef keyboard_QueryKeyboard_VK_HANJA
#define keyboard_QueryKeyboard_VK_HANJA 25L
#undef keyboard_QueryKeyboard_VK_KANJI
#define keyboard_QueryKeyboard_VK_KANJI 25L
#undef keyboard_QueryKeyboard_VK_ESCAPE
#define keyboard_QueryKeyboard_VK_ESCAPE 27L
#undef keyboard_QueryKeyboard_VK_CONVERT
#define keyboard_QueryKeyboard_VK_CONVERT 28L
#undef keyboard_QueryKeyboard_VK_NONCONVERT
#define keyboard_QueryKeyboard_VK_NONCONVERT 29L
#undef keyboard_QueryKeyboard_VK_ACCEPT
#define keyboard_QueryKeyboard_VK_ACCEPT 30L
#undef keyboard_QueryKeyboard_VK_MODECHANGE
#define keyboard_QueryKeyboard_VK_MODECHANGE 31L
#undef keyboard_QueryKeyboard_VK_SPACE
#define keyboard_QueryKeyboard_VK_SPACE 32L
#undef keyboard_QueryKeyboard_VK_PRIOR
#define keyboard_QueryKeyboard_VK_PRIOR 33L
#undef keyboard_QueryKeyboard_VK_NEXT
#define keyboard_QueryKeyboard_VK_NEXT 34L
#undef keyboard_QueryKeyboard_VK_END
#define keyboard_QueryKeyboard_VK_END 35L
#undef keyboard_QueryKeyboard_VK_HOME
#define keyboard_QueryKeyboard_VK_HOME 36L
#undef keyboard_QueryKeyboard_VK_LEFT
#define keyboard_QueryKeyboard_VK_LEFT 37L
#undef keyboard_QueryKeyboard_VK_UP
#define keyboard_QueryKeyboard_VK_UP 38L
#undef keyboard_QueryKeyboard_VK_RIGHT
#define keyboard_QueryKeyboard_VK_RIGHT 39L
#undef keyboard_QueryKeyboard_VK_DOWN
#define keyboard_QueryKeyboard_VK_DOWN 40L
#undef keyboard_QueryKeyboard_VK_SELECT
#define keyboard_QueryKeyboard_VK_SELECT 41L
#undef keyboard_QueryKeyboard_VK_PRINT
#define keyboard_QueryKeyboard_VK_PRINT 42L
#undef keyboard_QueryKeyboard_VK_EXECUTE
#define keyboard_QueryKeyboard_VK_EXECUTE 43L
#undef keyboard_QueryKeyboard_VK_SNAPSHOT
#define keyboard_QueryKeyboard_VK_SNAPSHOT 44L
#undef keyboard_QueryKeyboard_VK_INSERT
#define keyboard_QueryKeyboard_VK_INSERT 45L
#undef keyboard_QueryKeyboard_VK_DELETE
#define keyboard_QueryKeyboard_VK_DELETE 46L
#undef keyboard_QueryKeyboard_VK_HELP
#define keyboard_QueryKeyboard_VK_HELP 47L
#undef keyboard_QueryKeyboard_VK_0
#define keyboard_QueryKeyboard_VK_0 48L
#undef keyboard_QueryKeyboard_VK_1
#define keyboard_QueryKeyboard_VK_1 49L
#undef keyboard_QueryKeyboard_VK_2
#define keyboard_QueryKeyboard_VK_2 50L
#undef keyboard_QueryKeyboard_VK_3
#define keyboard_QueryKeyboard_VK_3 51L
#undef keyboard_QueryKeyboard_VK_4
#define keyboard_QueryKeyboard_VK_4 52L
#undef keyboard_QueryKeyboard_VK_5
#define keyboard_QueryKeyboard_VK_5 53L
#undef keyboard_QueryKeyboard_VK_6
#define keyboard_QueryKeyboard_VK_6 54L
#undef keyboard_QueryKeyboard_VK_7
#define keyboard_QueryKeyboard_VK_7 55L
#undef keyboard_QueryKeyboard_VK_8
#define keyboard_QueryKeyboard_VK_8 56L
#undef keyboard_QueryKeyboard_VK_9
#define keyboard_QueryKeyboard_VK_9 57L
#undef keyboard_QueryKeyboard_VK_A
#define keyboard_QueryKeyboard_VK_A 65L
#undef keyboard_QueryKeyboard_VK_B
#define keyboard_QueryKeyboard_VK_B 66L
#undef keyboard_QueryKeyboard_VK_C
#define keyboard_QueryKeyboard_VK_C 67L
#undef keyboard_QueryKeyboard_VK_D
#define keyboard_QueryKeyboard_VK_D 68L
#undef keyboard_QueryKeyboard_VK_E
#define keyboard_QueryKeyboard_VK_E 69L
#undef keyboard_QueryKeyboard_VK_F
#define keyboard_QueryKeyboard_VK_F 70L
#undef keyboard_QueryKeyboard_VK_G
#define keyboard_QueryKeyboard_VK_G 71L
#undef keyboard_QueryKeyboard_VK_H
#define keyboard_QueryKeyboard_VK_H 72L
#undef keyboard_QueryKeyboard_VK_I
#define keyboard_QueryKeyboard_VK_I 73L
#undef keyboard_QueryKeyboard_VK_J
#define keyboard_QueryKeyboard_VK_J 74L
#undef keyboard_QueryKeyboard_VK_K
#define keyboard_QueryKeyboard_VK_K 75L
#undef keyboard_QueryKeyboard_VK_L
#define keyboard_QueryKeyboard_VK_L 76L
#undef keyboard_QueryKeyboard_VK_M
#define keyboard_QueryKeyboard_VK_M 77L
#undef keyboard_QueryKeyboard_VK_N
#define keyboard_QueryKeyboard_VK_N 78L
#undef keyboard_QueryKeyboard_VK_O
#define keyboard_QueryKeyboard_VK_O 79L
#undef keyboard_QueryKeyboard_VK_P
#define keyboard_QueryKeyboard_VK_P 80L
#undef keyboard_QueryKeyboard_VK_Q
#define keyboard_QueryKeyboard_VK_Q 81L
#undef keyboard_QueryKeyboard_VK_R
#define keyboard_QueryKeyboard_VK_R 82L
#undef keyboard_QueryKeyboard_VK_S
#define keyboard_QueryKeyboard_VK_S 83L
#undef keyboard_QueryKeyboard_VK_T
#define keyboard_QueryKeyboard_VK_T 84L
#undef keyboard_QueryKeyboard_VK_U
#define keyboard_QueryKeyboard_VK_U 85L
#undef keyboard_QueryKeyboard_VK_V
#define keyboard_QueryKeyboard_VK_V 86L
#undef keyboard_QueryKeyboard_VK_W
#define keyboard_QueryKeyboard_VK_W 87L
#undef keyboard_QueryKeyboard_VK_X
#define keyboard_QueryKeyboard_VK_X 88L
#undef keyboard_QueryKeyboard_VK_Y
#define keyboard_QueryKeyboard_VK_Y 89L
#undef keyboard_QueryKeyboard_VK_Z
#define keyboard_QueryKeyboard_VK_Z 90L
#undef keyboard_QueryKeyboard_VK_LWIN
#define keyboard_QueryKeyboard_VK_LWIN 91L
#undef keyboard_QueryKeyboard_VK_RWIN
#define keyboard_QueryKeyboard_VK_RWIN 92L
#undef keyboard_QueryKeyboard_VK_APPS
#define keyboard_QueryKeyboard_VK_APPS 93L
#undef keyboard_QueryKeyboard_VK_SLEEP
#define keyboard_QueryKeyboard_VK_SLEEP 95L
#undef keyboard_QueryKeyboard_VK_NUMPAD0
#define keyboard_QueryKeyboard_VK_NUMPAD0 96L
#undef keyboard_QueryKeyboard_VK_NUMPAD1
#define keyboard_QueryKeyboard_VK_NUMPAD1 97L
#undef keyboard_QueryKeyboard_VK_NUMPAD2
#define keyboard_QueryKeyboard_VK_NUMPAD2 98L
#undef keyboard_QueryKeyboard_VK_NUMPAD3
#define keyboard_QueryKeyboard_VK_NUMPAD3 99L
#undef keyboard_QueryKeyboard_VK_NUMPAD4
#define keyboard_QueryKeyboard_VK_NUMPAD4 100L
#undef keyboard_QueryKeyboard_VK_NUMPAD5
#define keyboard_QueryKeyboard_VK_NUMPAD5 101L
#undef keyboard_QueryKeyboard_VK_NUMPAD6
#define keyboard_QueryKeyboard_VK_NUMPAD6 102L
#undef keyboard_QueryKeyboard_VK_NUMPAD7
#define keyboard_QueryKeyboard_VK_NUMPAD7 103L
#undef keyboard_QueryKeyboard_VK_NUMPAD8
#define keyboard_QueryKeyboard_VK_NUMPAD8 104L
#undef keyboard_QueryKeyboard_VK_NUMPAD9
#define keyboard_QueryKeyboard_VK_NUMPAD9 105L
#undef keyboard_QueryKeyboard_VK_MULTIPLY
#define keyboard_QueryKeyboard_VK_MULTIPLY 106L
#undef keyboard_QueryKeyboard_VK_ADD
#define keyboard_QueryKeyboard_VK_ADD 107L
#undef keyboard_QueryKeyboard_VK_SEPARATOR
#define keyboard_QueryKeyboard_VK_SEPARATOR 108L
#undef keyboard_QueryKeyboard_VK_SUBTRACT
#define keyboard_QueryKeyboard_VK_SUBTRACT 109L
#undef keyboard_QueryKeyboard_VK_DECIMAL
#define keyboard_QueryKeyboard_VK_DECIMAL 110L
#undef keyboard_QueryKeyboard_VK_DIVIDE
#define keyboard_QueryKeyboard_VK_DIVIDE 111L
#undef keyboard_QueryKeyboard_VK_F1
#define keyboard_QueryKeyboard_VK_F1 112L
#undef keyboard_QueryKeyboard_VK_F2
#define keyboard_QueryKeyboard_VK_F2 113L
#undef keyboard_QueryKeyboard_VK_F3
#define keyboard_QueryKeyboard_VK_F3 114L
#undef keyboard_QueryKeyboard_VK_F4
#define keyboard_QueryKeyboard_VK_F4 115L
#undef keyboard_QueryKeyboard_VK_F5
#define keyboard_QueryKeyboard_VK_F5 116L
#undef keyboard_QueryKeyboard_VK_F6
#define keyboard_QueryKeyboard_VK_F6 117L
#undef keyboard_QueryKeyboard_VK_F7
#define keyboard_QueryKeyboard_VK_F7 118L
#undef keyboard_QueryKeyboard_VK_F8
#define keyboard_QueryKeyboard_VK_F8 119L
#undef keyboard_QueryKeyboard_VK_F9
#define keyboard_QueryKeyboard_VK_F9 120L
#undef keyboard_QueryKeyboard_VK_F10
#define keyboard_QueryKeyboard_VK_F10 121L
#undef keyboard_QueryKeyboard_VK_F11
#define keyboard_QueryKeyboard_VK_F11 122L
#undef keyboard_QueryKeyboard_VK_F12
#define keyboard_QueryKeyboard_VK_F12 123L
#undef keyboard_QueryKeyboard_VK_F13
#define keyboard_QueryKeyboard_VK_F13 124L
#undef keyboard_QueryKeyboard_VK_F14
#define keyboard_QueryKeyboard_VK_F14 125L
#undef keyboard_QueryKeyboard_VK_F15
#define keyboard_QueryKeyboard_VK_F15 126L
#undef keyboard_QueryKeyboard_VK_F16
#define keyboard_QueryKeyboard_VK_F16 127L
#undef keyboard_QueryKeyboard_VK_F17
#define keyboard_QueryKeyboard_VK_F17 128L
#undef keyboard_QueryKeyboard_VK_F18
#define keyboard_QueryKeyboard_VK_F18 129L
#undef keyboard_QueryKeyboard_VK_F19
#define keyboard_QueryKeyboard_VK_F19 130L
#undef keyboard_QueryKeyboard_VK_F20
#define keyboard_QueryKeyboard_VK_F20 131L
#undef keyboard_QueryKeyboard_VK_F21
#define keyboard_QueryKeyboard_VK_F21 132L
#undef keyboard_QueryKeyboard_VK_F22
#define keyboard_QueryKeyboard_VK_F22 133L
#undef keyboard_QueryKeyboard_VK_F23
#define keyboard_QueryKeyboard_VK_F23 134L
#undef keyboard_QueryKeyboard_VK_F24
#define keyboard_QueryKeyboard_VK_F24 135L
#undef keyboard_QueryKeyboard_VK_NUMLOCK
#define keyboard_QueryKeyboard_VK_NUMLOCK 144L
#undef keyboard_QueryKeyboard_VK_SCROLL
#define keyboard_QueryKeyboard_VK_SCROLL 145L
#undef keyboard_QueryKeyboard_VK_LSHIFT
#define keyboard_QueryKeyboard_VK_LSHIFT 160L
#undef keyboard_QueryKeyboard_VK_RSHIFT
#define keyboard_QueryKeyboard_VK_RSHIFT 161L
#undef keyboard_QueryKeyboard_VK_LCONTROL
#define keyboard_QueryKeyboard_VK_LCONTROL 162L
#undef keyboard_QueryKeyboard_VK_RCONTROL
#define keyboard_QueryKeyboard_VK_RCONTROL 163L
#undef keyboard_QueryKeyboard_VK_LMENU
#define keyboard_QueryKeyboard_VK_LMENU 164L
#undef keyboard_QueryKeyboard_VK_RMENU
#define keyboard_QueryKeyboard_VK_RMENU 165L
#undef keyboard_QueryKeyboard_VK_BROWSER_BACK
#define keyboard_QueryKeyboard_VK_BROWSER_BACK 166L
#undef keyboard_QueryKeyboard_VK_BROWSER_FORWARD
#define keyboard_QueryKeyboard_VK_BROWSER_FORWARD 167L
#undef keyboard_QueryKeyboard_VK_BROWSER_REFRESH
#define keyboard_QueryKeyboard_VK_BROWSER_REFRESH 168L
#undef keyboard_QueryKeyboard_VK_BROWSER_STOP
#define keyboard_QueryKeyboard_VK_BROWSER_STOP 169L
#undef keyboard_QueryKeyboard_VK_BROWSER_SEARCH
#define keyboard_QueryKeyboard_VK_BROWSER_SEARCH 170L
#undef keyboard_QueryKeyboard_VK_BROWSER_FAVORITES
#define keyboard_QueryKeyboard_VK_BROWSER_FAVORITES 171L
#undef keyboard_QueryKeyboard_VK_BROWSER_HOME
#define keyboard_QueryKeyboard_VK_BROWSER_HOME 172L
#undef keyboard_QueryKeyboard_VK_VOLUME_MUTE
#define keyboard_QueryKeyboard_VK_VOLUME_MUTE 173L
#undef keyboard_QueryKeyboard_VK_VOLUME_DOWN
#define keyboard_QueryKeyboard_VK_VOLUME_DOWN 174L
#undef keyboard_QueryKeyboard_VK_VOLUME_UP
#define keyboard_QueryKeyboard_VK_VOLUME_UP 175L
#undef keyboard_QueryKeyboard_VK_MEDIA_NEXT_TRACK
#define keyboard_QueryKeyboard_VK_MEDIA_NEXT_TRACK 176L
#undef keyboard_QueryKeyboard_VK_MEDIA_PREV_TRACK
#define keyboard_QueryKeyboard_VK_MEDIA_PREV_TRACK 177L
#undef keyboard_QueryKeyboard_VK_MEDIA_STOP
#define keyboard_QueryKeyboard_VK_MEDIA_STOP 178L
#undef keyboard_QueryKeyboard_VK_MEDIA_PLAY_PAUSE
#define keyboard_QueryKeyboard_VK_MEDIA_PLAY_PAUSE 179L
#undef keyboard_QueryKeyboard_VK_LAUNCH_MAIL
#define keyboard_QueryKeyboard_VK_LAUNCH_MAIL 180L
#undef keyboard_QueryKeyboard_VK_LAUNCH_MEDIA_SELECT
#define keyboard_QueryKeyboard_VK_LAUNCH_MEDIA_SELECT 181L
#undef keyboard_QueryKeyboard_VK_LAUNCH_APP1
#define keyboard_QueryKeyboard_VK_LAUNCH_APP1 182L
#undef keyboard_QueryKeyboard_VK_LAUNCH_APP2
#define keyboard_QueryKeyboard_VK_LAUNCH_APP2 183L
#undef keyboard_QueryKeyboard_VK_OEM_1
#define keyboard_QueryKeyboard_VK_OEM_1 186L
#undef keyboard_QueryKeyboard_VK_OEM_PLUS
#define keyboard_QueryKeyboard_VK_OEM_PLUS 187L
#undef keyboard_QueryKeyboard_VK_OEM_COMMA
#define keyboard_QueryKeyboard_VK_OEM_COMMA 188L
#undef keyboard_QueryKeyboard_VK_OEM_MINUS
#define keyboard_QueryKeyboard_VK_OEM_MINUS 189L
#undef keyboard_QueryKeyboard_VK_OEM_PERIOD
#define keyboard_QueryKeyboard_VK_OEM_PERIOD 190L
#undef keyboard_QueryKeyboard_VK_OEM_2
#define keyboard_QueryKeyboard_VK_OEM_2 191L
#undef keyboard_QueryKeyboard_VK_OEM_3
#define keyboard_QueryKeyboard_VK_OEM_3 192L
#undef keyboard_QueryKeyboard_VK_OEM_4
#define keyboard_QueryKeyboard_VK_OEM_4 219L
#undef keyboard_QueryKeyboard_VK_OEM_5
#define keyboard_QueryKeyboard_VK_OEM_5 220L
#undef keyboard_QueryKeyboard_VK_OEM_6
#define keyboard_QueryKeyboard_VK_OEM_6 221L
#undef keyboard_QueryKeyboard_VK_OEM_7
#define keyboard_QueryKeyboard_VK_OEM_7 222L
#undef keyboard_QueryKeyboard_VK_OEM_8
#define keyboard_QueryKeyboard_VK_OEM_8 223L
#undef keyboard_QueryKeyboard_VK_OEM_102
#define keyboard_QueryKeyboard_VK_OEM_102 226L
#undef keyboard_QueryKeyboard_VK_PROCESSKEY
#define keyboard_QueryKeyboard_VK_PROCESSKEY 229L
#undef keyboard_QueryKeyboard_VK_PACKET
#define keyboard_QueryKeyboard_VK_PACKET 231L
#undef keyboard_QueryKeyboard_VK_ATTN
#define keyboard_QueryKeyboard_VK_ATTN 246L
#undef keyboard_QueryKeyboard_VK_CRSEL
#define keyboard_QueryKeyboard_VK_CRSEL 247L
#undef keyboard_QueryKeyboard_VK_EXSEL
#define keyboard_QueryKeyboard_VK_EXSEL 248L
#undef keyboard_QueryKeyboard_VK_EREOF
#define keyboard_QueryKeyboard_VK_EREOF 249L
#undef keyboard_QueryKeyboard_VK_PLAY
#define keyboard_QueryKeyboard_VK_PLAY 250L
#undef keyboard_QueryKeyboard_VK_ZOOM
#define keyboard_QueryKeyboard_VK_ZOOM 251L
#undef keyboard_QueryKeyboard_VK_NONAME
#define keyboard_QueryKeyboard_VK_NONAME 252L
#undef keyboard_QueryKeyboard_VK_PA1
#define keyboard_QueryKeyboard_VK_PA1 253L
#undef keyboard_QueryKeyboard_VK_OEM_CLEAR
#define keyboard_QueryKeyboard_VK_OEM_CLEAR 254L
/*
 * Class:     keyboard_QueryKeyboard
 * Method:    isKeyPressed
 * Signature: (I)Z
 */
JNIEXPORT jboolean JNICALL Java_keyboard_QueryKeyboard_isKeyPressed
  (JNIEnv *, jclass, jint);
 
#ifdef __cplusplus
}
#endif
#endif

#include "keyboard_QueryKeyboard.h"
 
/*
 * Class:     keyboard_QueryKeyboard
 * Method:    isKeyPressed
 * Signature: (I)Z
 */
JNIEXPORT jboolean JNICALL Java_keyboard_QueryKeyboard_isKeyPressed
  (JNIEnv *env, jclass c, jint keycode)
{
	// if MSB is set, button is being pressed
	return (GetAsyncKeyState(keycode) & (1 << (8*sizeof(short)))) != 0;
}

For those familiar with the C++ windows code, the native code uses GetAsyncKeyState. Technically, any integer value can be inputted. However, to help the Java developer, I copied the table of used virtual key codes and put them as static constants in the class. Also, the native code will only determine if a key is currently being pressed or not. The section in GetAsyncKeyState about the state since the last call to that method can be completely ignored (this isn't safe to rely on in modern Windows programming, anyways).

Usage of this code:

Here's a small snippet of code which uses the above library. It listens for which of the arrow keys is currently being pressed, at 50 ms increments. If the escape key is pressed, the program exits.
	public static void main(String[] args) throws InterruptedException
	{
		while (!QueryKeyboard.isKeyPressed(QueryKeyboard.VK_ESCAPE))
		{
			boolean left = QueryKeyboard.isKeyPressed(QueryKeyboard.VK_LEFT);
			boolean right = QueryKeyboard.isKeyPressed(QueryKeyboard.VK_RIGHT);
			boolean up = QueryKeyboard.isKeyPressed(QueryKeyboard.VK_UP);
			boolean down = QueryKeyboard.isKeyPressed(QueryKeyboard.VK_DOWN);
			if (left)
			{
				System.out.print("left ");
			}
			if (right)
			{
				System.out.print("right ");
			}
			if (up)
			{
				System.out.print("up ");
			}
			if (down)
			{
				System.out.print("down ");
			}
			if (left || right || up || down)
			{
				System.out.println();
			}
			Thread.sleep(50);
		}
		System.out.println("end.");
	}

Normally, something like this would be impossible in Java. However, thanks to the magic of JNI, it's possible.

Final note
You're welcome to use this code however you want, it's provided as-is.

Happy Coding

Updated June 4th, 2011 at 03:08 AM by helloworld922

Categories
Uncategorized

Comments