Added keyboard translation to linux and win32

This commit is contained in:
Elias Naur 2003-01-11 23:09:38 +00:00
parent a6bd8e7fa4
commit 1b3d0278f9
3 changed files with 387 additions and 169 deletions

View file

@ -33,6 +33,7 @@
package org.lwjgl.input;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import org.lwjgl.Display;
import org.lwjgl.Sys;
@ -49,6 +50,18 @@ import org.lwjgl.Sys;
* @version $Revision$
*/
public class Keyboard {
/**
* The special character meaning that no
* character was translated for the event.
*/
public static final char CHAR_NONE = '\0';
/**
* The special keycode meaning that only the
* translated character is valid.
*/
public static final int KEY_NONE = 0x00;
public static final int KEY_ESCAPE = 0x01;
public static final int KEY_1 = 0x02;
public static final int KEY_2 = 0x03;
@ -173,132 +186,6 @@ public class Keyboard {
public static final int KEY_POWER = 0xDE;
public static final int KEY_SLEEP = 0xDF;
// Maps keycodes to ascii characters
private static final char map[] =
{
0,
0,
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'0',
'-',
'=',
0,
'\t',
'q',
'w',
'e',
'r',
't',
'y',
'u',
'i',
'o',
'p',
'[',
']',
'\n',
0,
'a',
's',
'd',
'f',
'g',
'h',
'j',
'k',
'l',
';',
'\'',
'#',
0,
'\\',
'z',
'x',
'c',
'v',
'b',
'n',
'm',
',',
'.',
'/',
0,
0,
0,
' ' };
private static final char shiftMap[] =
{
0,
0,
'!',
'"',
'*',
'$',
'%',
'^',
'&',
'*',
'(',
')',
'_',
'+',
0,
'\t',
'Q',
'W',
'E',
'R',
'T',
'Y',
'U',
'I',
'O',
'P',
'{',
'}',
'\n',
0,
'A',
'S',
'D',
'F',
'G',
'H',
'J',
'K',
'L',
':',
'@',
'~',
0,
'|',
'Z',
'X',
'C',
'V',
'B',
'N',
'M',
'<',
'>',
'?',
0,
0,
0,
' ' };
static {
initialize();
}
/** Has the keyboard been created? */
private static boolean created;
@ -310,12 +197,19 @@ public class Keyboard {
/**
* The key events from the last read: a sequence of pairs of key number,
* followed by state.
* followed by state. If translation is enabled, the state is followed by
* a 2 byte java char representing the translated character.
*/
private static ByteBuffer readBuffer;
/** Address of the read buffer */
private static int readBufferAddress;
/** True if translation is enabled */
private static boolean translationEnabled;
/** The number of events read in the last read() */
private static int numEvents;
/** The current keyboard character being examined */
public static char character;
/** The current keyboard event key being examined */
public static int key;
@ -323,8 +217,12 @@ public class Keyboard {
/** The current state of the key being examined in the event queue */
public static boolean state;
static {
initialize();
}
/**
* Mouse cannot be constructed.
* Keyboard cannot be constructed.
*/
private Keyboard() {
}
@ -376,7 +274,7 @@ public class Keyboard {
}
/**
* Native method the destroy the keyboard
* Native method to destroy the keyboard
*/
private static native void nDestroy();
@ -402,17 +300,35 @@ public class Keyboard {
public static void read() {
assert created : "The keyboard has not been created.";
assert readBuffer != null : "Keyboard buffering has not been enabled.";
numEvents = nRead();
readBuffer.clear();
readBuffer.limit(nRead(readBufferAddress) << 1);
if (translationEnabled)
readBuffer.limit(numEvents << 2);
else
readBuffer.limit(numEvents << 1);
}
/**
* Native method to read the keyboard buffer
*
* @param readBufferAddress the address of the keyboard buffer
* @return the number of keyboard events read
* @return the total number of events read.
*/
private static native int nRead(int readBufferAddress);
private static native int nRead();
/**
* Enable keyboard translation. Must be called after the keyboard is created,
* and keyboard buffering must be enabled.
*/
public static void enableTranslation() {
assert created : "The keyboard has not been created.";
assert readBuffer != null : "Keyboard buffering has not been enabled.";
nEnableTranslation();
translationEnabled = true;
}
/**
* Native method to enable the translation buffer
*/
private static native void nEnableTranslation();
/**
* Enable keyboard buffering. Must be called after the keyboard is created.
@ -421,7 +337,10 @@ public class Keyboard {
*/
public static int enableBuffer() {
assert created : "The keyboard has not been created.";
return nEnableBuffer();
int buf_len = nEnableBuffer();
if (readBuffer != null)
readBuffer.order(ByteOrder.nativeOrder());
return buf_len;
}
/**
@ -446,7 +365,10 @@ public class Keyboard {
* @return the number of keyboard events
*/
public static int getNumKeyboardEvents() {
return readBuffer.limit() >> 1;
assert created : "The keyboard has not been created.";
assert readBuffer != null : "Keyboard buffering has not been enabled.";
return numEvents;
}
/**
@ -461,27 +383,10 @@ public class Keyboard {
if (readBuffer.hasRemaining()) {
key = readBuffer.get() & 0xFF;
state = readBuffer.get() != 0;
if (translationEnabled)
character = readBuffer.getChar();
return true;
} else
return false;
}
/**
* Maps a keycode to a character.
* @param keyCode The keycode
* @return the corresponding ASCII character or 0 if no mapping is possible
*/
public static char map(int keyCode) {
char c;
if (keyCode < 0 || keyCode >= map.length)
return 0;
else if (isKeyDown(KEY_LSHIFT) || isKeyDown(KEY_RSHIFT)) {
c = shiftMap[keyCode];
} else {
c = map[keyCode];
}
return c;
}
}

View file

@ -8,12 +8,266 @@
extern "C" {
#endif
/* Inaccessible static: _00024assertionsDisabled */
#undef org_lwjgl_input_Keyboard_CHAR_NONE
#define org_lwjgl_input_Keyboard_CHAR_NONE 0L
#undef org_lwjgl_input_Keyboard_KEY_NONE
#define org_lwjgl_input_Keyboard_KEY_NONE 0L
#undef org_lwjgl_input_Keyboard_KEY_ESCAPE
#define org_lwjgl_input_Keyboard_KEY_ESCAPE 1L
#undef org_lwjgl_input_Keyboard_KEY_1
#define org_lwjgl_input_Keyboard_KEY_1 2L
#undef org_lwjgl_input_Keyboard_KEY_2
#define org_lwjgl_input_Keyboard_KEY_2 3L
#undef org_lwjgl_input_Keyboard_KEY_3
#define org_lwjgl_input_Keyboard_KEY_3 4L
#undef org_lwjgl_input_Keyboard_KEY_4
#define org_lwjgl_input_Keyboard_KEY_4 5L
#undef org_lwjgl_input_Keyboard_KEY_5
#define org_lwjgl_input_Keyboard_KEY_5 6L
#undef org_lwjgl_input_Keyboard_KEY_6
#define org_lwjgl_input_Keyboard_KEY_6 7L
#undef org_lwjgl_input_Keyboard_KEY_7
#define org_lwjgl_input_Keyboard_KEY_7 8L
#undef org_lwjgl_input_Keyboard_KEY_8
#define org_lwjgl_input_Keyboard_KEY_8 9L
#undef org_lwjgl_input_Keyboard_KEY_9
#define org_lwjgl_input_Keyboard_KEY_9 10L
#undef org_lwjgl_input_Keyboard_KEY_0
#define org_lwjgl_input_Keyboard_KEY_0 11L
#undef org_lwjgl_input_Keyboard_KEY_MINUS
#define org_lwjgl_input_Keyboard_KEY_MINUS 12L
#undef org_lwjgl_input_Keyboard_KEY_EQUALS
#define org_lwjgl_input_Keyboard_KEY_EQUALS 13L
#undef org_lwjgl_input_Keyboard_KEY_BACK
#define org_lwjgl_input_Keyboard_KEY_BACK 14L
#undef org_lwjgl_input_Keyboard_KEY_TAB
#define org_lwjgl_input_Keyboard_KEY_TAB 15L
#undef org_lwjgl_input_Keyboard_KEY_Q
#define org_lwjgl_input_Keyboard_KEY_Q 16L
#undef org_lwjgl_input_Keyboard_KEY_W
#define org_lwjgl_input_Keyboard_KEY_W 17L
#undef org_lwjgl_input_Keyboard_KEY_E
#define org_lwjgl_input_Keyboard_KEY_E 18L
#undef org_lwjgl_input_Keyboard_KEY_R
#define org_lwjgl_input_Keyboard_KEY_R 19L
#undef org_lwjgl_input_Keyboard_KEY_T
#define org_lwjgl_input_Keyboard_KEY_T 20L
#undef org_lwjgl_input_Keyboard_KEY_Y
#define org_lwjgl_input_Keyboard_KEY_Y 21L
#undef org_lwjgl_input_Keyboard_KEY_U
#define org_lwjgl_input_Keyboard_KEY_U 22L
#undef org_lwjgl_input_Keyboard_KEY_I
#define org_lwjgl_input_Keyboard_KEY_I 23L
#undef org_lwjgl_input_Keyboard_KEY_O
#define org_lwjgl_input_Keyboard_KEY_O 24L
#undef org_lwjgl_input_Keyboard_KEY_P
#define org_lwjgl_input_Keyboard_KEY_P 25L
#undef org_lwjgl_input_Keyboard_KEY_LBRACKET
#define org_lwjgl_input_Keyboard_KEY_LBRACKET 26L
#undef org_lwjgl_input_Keyboard_KEY_RBRACKET
#define org_lwjgl_input_Keyboard_KEY_RBRACKET 27L
#undef org_lwjgl_input_Keyboard_KEY_RETURN
#define org_lwjgl_input_Keyboard_KEY_RETURN 28L
#undef org_lwjgl_input_Keyboard_KEY_LCONTROL
#define org_lwjgl_input_Keyboard_KEY_LCONTROL 29L
#undef org_lwjgl_input_Keyboard_KEY_A
#define org_lwjgl_input_Keyboard_KEY_A 30L
#undef org_lwjgl_input_Keyboard_KEY_S
#define org_lwjgl_input_Keyboard_KEY_S 31L
#undef org_lwjgl_input_Keyboard_KEY_D
#define org_lwjgl_input_Keyboard_KEY_D 32L
#undef org_lwjgl_input_Keyboard_KEY_F
#define org_lwjgl_input_Keyboard_KEY_F 33L
#undef org_lwjgl_input_Keyboard_KEY_G
#define org_lwjgl_input_Keyboard_KEY_G 34L
#undef org_lwjgl_input_Keyboard_KEY_H
#define org_lwjgl_input_Keyboard_KEY_H 35L
#undef org_lwjgl_input_Keyboard_KEY_J
#define org_lwjgl_input_Keyboard_KEY_J 36L
#undef org_lwjgl_input_Keyboard_KEY_K
#define org_lwjgl_input_Keyboard_KEY_K 37L
#undef org_lwjgl_input_Keyboard_KEY_L
#define org_lwjgl_input_Keyboard_KEY_L 38L
#undef org_lwjgl_input_Keyboard_KEY_SEMICOLON
#define org_lwjgl_input_Keyboard_KEY_SEMICOLON 39L
#undef org_lwjgl_input_Keyboard_KEY_APOSTROPHE
#define org_lwjgl_input_Keyboard_KEY_APOSTROPHE 40L
#undef org_lwjgl_input_Keyboard_KEY_GRAVE
#define org_lwjgl_input_Keyboard_KEY_GRAVE 41L
#undef org_lwjgl_input_Keyboard_KEY_LSHIFT
#define org_lwjgl_input_Keyboard_KEY_LSHIFT 42L
#undef org_lwjgl_input_Keyboard_KEY_BACKSLASH
#define org_lwjgl_input_Keyboard_KEY_BACKSLASH 43L
#undef org_lwjgl_input_Keyboard_KEY_Z
#define org_lwjgl_input_Keyboard_KEY_Z 44L
#undef org_lwjgl_input_Keyboard_KEY_X
#define org_lwjgl_input_Keyboard_KEY_X 45L
#undef org_lwjgl_input_Keyboard_KEY_C
#define org_lwjgl_input_Keyboard_KEY_C 46L
#undef org_lwjgl_input_Keyboard_KEY_V
#define org_lwjgl_input_Keyboard_KEY_V 47L
#undef org_lwjgl_input_Keyboard_KEY_B
#define org_lwjgl_input_Keyboard_KEY_B 48L
#undef org_lwjgl_input_Keyboard_KEY_N
#define org_lwjgl_input_Keyboard_KEY_N 49L
#undef org_lwjgl_input_Keyboard_KEY_M
#define org_lwjgl_input_Keyboard_KEY_M 50L
#undef org_lwjgl_input_Keyboard_KEY_COMMA
#define org_lwjgl_input_Keyboard_KEY_COMMA 51L
#undef org_lwjgl_input_Keyboard_KEY_PERIOD
#define org_lwjgl_input_Keyboard_KEY_PERIOD 52L
#undef org_lwjgl_input_Keyboard_KEY_SLASH
#define org_lwjgl_input_Keyboard_KEY_SLASH 53L
#undef org_lwjgl_input_Keyboard_KEY_RSHIFT
#define org_lwjgl_input_Keyboard_KEY_RSHIFT 54L
#undef org_lwjgl_input_Keyboard_KEY_MULTIPLY
#define org_lwjgl_input_Keyboard_KEY_MULTIPLY 55L
#undef org_lwjgl_input_Keyboard_KEY_LMENU
#define org_lwjgl_input_Keyboard_KEY_LMENU 56L
#undef org_lwjgl_input_Keyboard_KEY_SPACE
#define org_lwjgl_input_Keyboard_KEY_SPACE 57L
#undef org_lwjgl_input_Keyboard_KEY_CAPITAL
#define org_lwjgl_input_Keyboard_KEY_CAPITAL 58L
#undef org_lwjgl_input_Keyboard_KEY_F1
#define org_lwjgl_input_Keyboard_KEY_F1 59L
#undef org_lwjgl_input_Keyboard_KEY_F2
#define org_lwjgl_input_Keyboard_KEY_F2 60L
#undef org_lwjgl_input_Keyboard_KEY_F3
#define org_lwjgl_input_Keyboard_KEY_F3 61L
#undef org_lwjgl_input_Keyboard_KEY_F4
#define org_lwjgl_input_Keyboard_KEY_F4 62L
#undef org_lwjgl_input_Keyboard_KEY_F5
#define org_lwjgl_input_Keyboard_KEY_F5 63L
#undef org_lwjgl_input_Keyboard_KEY_F6
#define org_lwjgl_input_Keyboard_KEY_F6 64L
#undef org_lwjgl_input_Keyboard_KEY_F7
#define org_lwjgl_input_Keyboard_KEY_F7 65L
#undef org_lwjgl_input_Keyboard_KEY_F8
#define org_lwjgl_input_Keyboard_KEY_F8 66L
#undef org_lwjgl_input_Keyboard_KEY_F9
#define org_lwjgl_input_Keyboard_KEY_F9 67L
#undef org_lwjgl_input_Keyboard_KEY_F10
#define org_lwjgl_input_Keyboard_KEY_F10 68L
#undef org_lwjgl_input_Keyboard_KEY_NUMLOCK
#define org_lwjgl_input_Keyboard_KEY_NUMLOCK 69L
#undef org_lwjgl_input_Keyboard_KEY_SCROLL
#define org_lwjgl_input_Keyboard_KEY_SCROLL 70L
#undef org_lwjgl_input_Keyboard_KEY_NUMPAD7
#define org_lwjgl_input_Keyboard_KEY_NUMPAD7 71L
#undef org_lwjgl_input_Keyboard_KEY_NUMPAD8
#define org_lwjgl_input_Keyboard_KEY_NUMPAD8 72L
#undef org_lwjgl_input_Keyboard_KEY_NUMPAD9
#define org_lwjgl_input_Keyboard_KEY_NUMPAD9 73L
#undef org_lwjgl_input_Keyboard_KEY_SUBTRACT
#define org_lwjgl_input_Keyboard_KEY_SUBTRACT 74L
#undef org_lwjgl_input_Keyboard_KEY_NUMPAD4
#define org_lwjgl_input_Keyboard_KEY_NUMPAD4 75L
#undef org_lwjgl_input_Keyboard_KEY_NUMPAD5
#define org_lwjgl_input_Keyboard_KEY_NUMPAD5 76L
#undef org_lwjgl_input_Keyboard_KEY_NUMPAD6
#define org_lwjgl_input_Keyboard_KEY_NUMPAD6 77L
#undef org_lwjgl_input_Keyboard_KEY_ADD
#define org_lwjgl_input_Keyboard_KEY_ADD 78L
#undef org_lwjgl_input_Keyboard_KEY_NUMPAD1
#define org_lwjgl_input_Keyboard_KEY_NUMPAD1 79L
#undef org_lwjgl_input_Keyboard_KEY_NUMPAD2
#define org_lwjgl_input_Keyboard_KEY_NUMPAD2 80L
#undef org_lwjgl_input_Keyboard_KEY_NUMPAD3
#define org_lwjgl_input_Keyboard_KEY_NUMPAD3 81L
#undef org_lwjgl_input_Keyboard_KEY_NUMPAD0
#define org_lwjgl_input_Keyboard_KEY_NUMPAD0 82L
#undef org_lwjgl_input_Keyboard_KEY_DECIMAL
#define org_lwjgl_input_Keyboard_KEY_DECIMAL 83L
#undef org_lwjgl_input_Keyboard_KEY_F11
#define org_lwjgl_input_Keyboard_KEY_F11 87L
#undef org_lwjgl_input_Keyboard_KEY_F12
#define org_lwjgl_input_Keyboard_KEY_F12 88L
#undef org_lwjgl_input_Keyboard_KEY_F13
#define org_lwjgl_input_Keyboard_KEY_F13 100L
#undef org_lwjgl_input_Keyboard_KEY_F14
#define org_lwjgl_input_Keyboard_KEY_F14 101L
#undef org_lwjgl_input_Keyboard_KEY_F15
#define org_lwjgl_input_Keyboard_KEY_F15 102L
#undef org_lwjgl_input_Keyboard_KEY_KANA
#define org_lwjgl_input_Keyboard_KEY_KANA 112L
#undef org_lwjgl_input_Keyboard_KEY_CONVERT
#define org_lwjgl_input_Keyboard_KEY_CONVERT 121L
#undef org_lwjgl_input_Keyboard_KEY_NOCONVERT
#define org_lwjgl_input_Keyboard_KEY_NOCONVERT 123L
#undef org_lwjgl_input_Keyboard_KEY_YEN
#define org_lwjgl_input_Keyboard_KEY_YEN 125L
#undef org_lwjgl_input_Keyboard_KEY_NUMPADEQUALS
#define org_lwjgl_input_Keyboard_KEY_NUMPADEQUALS 141L
#undef org_lwjgl_input_Keyboard_KEY_CIRCUMFLEX
#define org_lwjgl_input_Keyboard_KEY_CIRCUMFLEX 144L
#undef org_lwjgl_input_Keyboard_KEY_AT
#define org_lwjgl_input_Keyboard_KEY_AT 145L
#undef org_lwjgl_input_Keyboard_KEY_COLON
#define org_lwjgl_input_Keyboard_KEY_COLON 146L
#undef org_lwjgl_input_Keyboard_KEY_UNDERLINE
#define org_lwjgl_input_Keyboard_KEY_UNDERLINE 147L
#undef org_lwjgl_input_Keyboard_KEY_KANJI
#define org_lwjgl_input_Keyboard_KEY_KANJI 148L
#undef org_lwjgl_input_Keyboard_KEY_STOP
#define org_lwjgl_input_Keyboard_KEY_STOP 149L
#undef org_lwjgl_input_Keyboard_KEY_AX
#define org_lwjgl_input_Keyboard_KEY_AX 150L
#undef org_lwjgl_input_Keyboard_KEY_UNLABELED
#define org_lwjgl_input_Keyboard_KEY_UNLABELED 151L
#undef org_lwjgl_input_Keyboard_KEY_NUMPADENTER
#define org_lwjgl_input_Keyboard_KEY_NUMPADENTER 156L
#undef org_lwjgl_input_Keyboard_KEY_RCONTROL
#define org_lwjgl_input_Keyboard_KEY_RCONTROL 157L
#undef org_lwjgl_input_Keyboard_KEY_NUMPADCOMMA
#define org_lwjgl_input_Keyboard_KEY_NUMPADCOMMA 179L
#undef org_lwjgl_input_Keyboard_KEY_DIVIDE
#define org_lwjgl_input_Keyboard_KEY_DIVIDE 181L
#undef org_lwjgl_input_Keyboard_KEY_SYSRQ
#define org_lwjgl_input_Keyboard_KEY_SYSRQ 183L
#undef org_lwjgl_input_Keyboard_KEY_RMENU
#define org_lwjgl_input_Keyboard_KEY_RMENU 184L
#undef org_lwjgl_input_Keyboard_KEY_PAUSE
#define org_lwjgl_input_Keyboard_KEY_PAUSE 197L
#undef org_lwjgl_input_Keyboard_KEY_HOME
#define org_lwjgl_input_Keyboard_KEY_HOME 199L
#undef org_lwjgl_input_Keyboard_KEY_UP
#define org_lwjgl_input_Keyboard_KEY_UP 200L
#undef org_lwjgl_input_Keyboard_KEY_PRIOR
#define org_lwjgl_input_Keyboard_KEY_PRIOR 201L
#undef org_lwjgl_input_Keyboard_KEY_LEFT
#define org_lwjgl_input_Keyboard_KEY_LEFT 203L
#undef org_lwjgl_input_Keyboard_KEY_RIGHT
#define org_lwjgl_input_Keyboard_KEY_RIGHT 205L
#undef org_lwjgl_input_Keyboard_KEY_END
#define org_lwjgl_input_Keyboard_KEY_END 207L
#undef org_lwjgl_input_Keyboard_KEY_DOWN
#define org_lwjgl_input_Keyboard_KEY_DOWN 208L
#undef org_lwjgl_input_Keyboard_KEY_NEXT
#define org_lwjgl_input_Keyboard_KEY_NEXT 209L
#undef org_lwjgl_input_Keyboard_KEY_INSERT
#define org_lwjgl_input_Keyboard_KEY_INSERT 210L
#undef org_lwjgl_input_Keyboard_KEY_DELETE
#define org_lwjgl_input_Keyboard_KEY_DELETE 211L
#undef org_lwjgl_input_Keyboard_KEY_LWIN
#define org_lwjgl_input_Keyboard_KEY_LWIN 219L
#undef org_lwjgl_input_Keyboard_KEY_RWIN
#define org_lwjgl_input_Keyboard_KEY_RWIN 220L
#undef org_lwjgl_input_Keyboard_KEY_APPS
#define org_lwjgl_input_Keyboard_KEY_APPS 221L
#undef org_lwjgl_input_Keyboard_KEY_POWER
#define org_lwjgl_input_Keyboard_KEY_POWER 222L
#undef org_lwjgl_input_Keyboard_KEY_SLEEP
#define org_lwjgl_input_Keyboard_KEY_SLEEP 223L
/* Inaccessible static: created */
/* Inaccessible static: keyDownBuffer */
/* Inaccessible static: keyDownAddress */
/* Inaccessible static: readBuffer */
/* Inaccessible static: readBufferAddress */
/* Inaccessible static: class_000240 */
/* Inaccessible static: translationEnabled */
/* Inaccessible static: numEvents */
/* Inaccessible static: character */
/* Inaccessible static: key */
/* Inaccessible static: state */
/* Inaccessible static: class_00024org_00024lwjgl_00024input_00024Keyboard */
/*
* Class: org_lwjgl_input_Keyboard
* Method: initIDs
@ -49,10 +303,18 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Keyboard_nPoll
/*
* Class: org_lwjgl_input_Keyboard
* Method: nRead
* Signature: (I)V
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_org_lwjgl_input_Keyboard_nRead
(JNIEnv *, jclass, jint);
(JNIEnv *, jclass);
/*
* Class: org_lwjgl_input_Keyboard
* Method: nEnableTranslation
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_input_Keyboard_nEnableTranslation
(JNIEnv *, jclass);
/*
* Class: org_lwjgl_input_Keyboard

View file

@ -50,7 +50,7 @@
BYTE readBuffer[KEYBOARD_BUFFER_SIZE];
LPDIRECTINPUTDEVICE lpdiKeyboard = NULL;
jfieldID fid_readBuffer;
jfieldID fid_readBufferAddress;
bool translationEnabled;
extern LPDIRECTINPUT lpdi;
extern HWND hwnd;
@ -70,7 +70,6 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Keyboard_initIDs
}
fid_readBuffer = env->GetStaticFieldID(clazz, "readBuffer", "Ljava/nio/ByteBuffer;");
fid_readBufferAddress = env->GetStaticFieldID(clazz, "readBufferAddress", "I");
}
/*
@ -81,6 +80,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Keyboard_initIDs
JNIEXPORT jboolean JNICALL Java_org_lwjgl_input_Keyboard_nCreate
(JNIEnv * env, jclass clazz)
{
translationEnabled = false;
// Check to see if we're already initialized
if (lpdiKeyboard != NULL) {
printf("Keyboard already created.\n");
@ -180,13 +180,20 @@ JNIEXPORT void JNICALL Java_org_lwjgl_input_Keyboard_nPoll
* Signature: (I)V
*/
JNIEXPORT jint JNICALL Java_org_lwjgl_input_Keyboard_nRead
(JNIEnv * env, jclass clazz, jint keys)
(JNIEnv * env, jclass clazz)
{
static DIDEVICEOBJECTDATA rgdod[KEYBOARD_BUFFER_SIZE];
wchar_t transBuf[KEYBOARD_BUFFER_SIZE];
MSG msg;
BYTE state[256];
DWORD bufsize = KEYBOARD_BUFFER_SIZE;
HRESULT ret;
int num_chars;
int num_events = 0;
do {
ret = lpdiKeyboard->Acquire();
} while (ret != DI_OK && ret != S_FALSE);
@ -198,13 +205,47 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_input_Keyboard_nRead
0);
if (ret == DI_OK) {
unsigned char * buf = (unsigned char *) keys;
unsigned char * buf = readBuffer;
for (unsigned int i = 0; i < bufsize; i ++) {
num_events++;
*buf++ = (unsigned char) rgdod[i].dwOfs;
*buf++ = (unsigned char) rgdod[i].dwData;
if (translationEnabled) {
while (PeekMessage(&msg, hwnd, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE) != 0)
; // Flush keyboard messages to update keyboard state
UINT virt_key = MapVirtualKey(rgdod[i].dwOfs, 1);
if (virt_key != 0) {
if (!GetKeyboardState(state))
return -1;
num_chars = ToUnicode(virt_key,
rgdod[i].dwOfs,
state,
transBuf,
KEYBOARD_BUFFER_SIZE, 0);
if (num_chars > 0) {
wchar_t ch = transBuf[0];
*buf++ = (unsigned char) (ch & 0xff);
*buf++ = (unsigned char) ((ch & 0xff00) >> 8);
for (int i = 1; i < num_chars; i++) {
num_events++;
ch = transBuf[i];
*buf++ = 0;
*buf++ = 0;
*buf++ = (unsigned char) (ch & 0xff);
*buf++ = (unsigned char) ((ch & 0xff00) >> 8);
}
} else {
*buf++ = 0;
*buf++ = 0;
}
} else {
*buf++ = 0;
*buf++ = 0;
}
}
}
return bufsize;
} if (ret == DI_BUFFEROVERFLOW) {
return num_events;
} else if (ret == DI_BUFFEROVERFLOW) {
#ifdef _DEBUG
printf("Keyboard buffer overflowed\n");
#endif
@ -242,6 +283,17 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_input_Keyboard_nRead
}
}
/*
* Class: org_lwjgl_input_Keyboard
* Method: nEnableTranslation
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_lwjgl_input_Keyboard_nEnableTranslation
(JNIEnv *, jclass)
{
translationEnabled = true;
}
/*
* Class: org_lwjgl_input_Keyboard
* Method: nEnableBuffer
@ -252,6 +304,5 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_input_Keyboard_nEnableBuffer
{
jobject newBuffer = env->NewDirectByteBuffer(&readBuffer, KEYBOARD_BUFFER_SIZE);
env->SetStaticObjectField(clazz, fid_readBuffer, newBuffer);
env->SetStaticIntField(clazz, fid_readBufferAddress, (jint) (&readBuffer));
return KEYBOARD_BUFFER_SIZE;
}