2 * ReactOS W32 Subsystem
3 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS kernel
24 * FILE: subsys/win32k/ntuser/keyboard.c
25 * PROGRAMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
27 * 06-06-2001 CSH Created
30 /* INCLUDES ******************************************************************/
37 /* Directory to load key layouts from */
38 #define SYSTEMROOT_DIR L"\\SystemRoot\\System32\\"
40 #define CAPITAL_BIT 0x80000000
41 #define NUMLOCK_BIT 0x40000000
42 #define MOD_BITS_MASK 0x3fffffff
43 #define MOD_KCTRL 0x02
45 #define KS_DOWN_MASK 0xc0
46 #define KS_DOWN_BIT 0x80
47 #define KS_LOCK_BIT 0x01
49 #define LP_EXT_BIT (1<<24)
50 /* From kbdxx.c -- Key changes with numlock */
53 /* Lock the keyboard state to prevent unusual concurrent access */
54 FAST_MUTEX QueueStateLock
;
56 BYTE QueueKeyStateTable
[256];
58 #define IntLockQueueState \
59 ExAcquireFastMutex(&QueueStateLock)
61 #define IntUnLockQueueState \
62 ExReleaseFastMutex(&QueueStateLock)
64 /* FUNCTIONS *****************************************************************/
66 /* Initialization -- Right now, just zero the key state and init the lock */
67 NTSTATUS FASTCALL
InitKeyboardImpl(VOID
) {
68 ExInitializeFastMutex(&QueueStateLock
);
69 RtlZeroMemory(&QueueKeyStateTable
,0x100);
70 return STATUS_SUCCESS
;
73 /*** Statics used by TranslateMessage ***/
75 /*** Shift state code was out of hand, sorry. --- arty */
77 static UINT
DontDistinguishShifts( UINT ret
) {
78 if( ret
== VK_LSHIFT
|| ret
== VK_RSHIFT
) ret
= VK_LSHIFT
;
79 if( ret
== VK_LCONTROL
|| ret
== VK_RCONTROL
) ret
= VK_LCONTROL
;
80 if( ret
== VK_LMENU
|| ret
== VK_RMENU
) ret
= VK_LMENU
;
84 static VOID STDCALL
SetKeyState(DWORD key
, DWORD vk
, DWORD ext
, BOOL down
) {
87 /* Special handling for toggles like numpad and caps lock */
88 if (vk
== VK_CAPITAL
|| vk
== VK_NUMLOCK
) {
89 if (down
) QueueKeyStateTable
[vk
] ^= KS_LOCK_BIT
;
92 if (ext
&& vk
== VK_LSHIFT
)
94 if (ext
&& vk
== VK_LCONTROL
)
96 if (ext
&& vk
== VK_LMENU
)
100 QueueKeyStateTable
[vk
] |= KS_DOWN_BIT
;
102 QueueKeyStateTable
[vk
] &= ~KS_DOWN_MASK
;
104 if (vk
== VK_LSHIFT
|| vk
== VK_RSHIFT
) {
105 if ((QueueKeyStateTable
[VK_LSHIFT
] & KS_DOWN_BIT
) ||
106 (QueueKeyStateTable
[VK_RSHIFT
] & KS_DOWN_BIT
)) {
107 QueueKeyStateTable
[VK_SHIFT
] |= KS_DOWN_BIT
;
109 QueueKeyStateTable
[VK_SHIFT
] &= ~KS_DOWN_MASK
;
113 if (vk
== VK_LCONTROL
|| vk
== VK_RCONTROL
) {
114 if ((QueueKeyStateTable
[VK_LCONTROL
] & KS_DOWN_BIT
) ||
115 (QueueKeyStateTable
[VK_RCONTROL
] & KS_DOWN_BIT
)) {
116 QueueKeyStateTable
[VK_CONTROL
] |= KS_DOWN_BIT
;
118 QueueKeyStateTable
[VK_CONTROL
] &= ~KS_DOWN_MASK
;
122 if (vk
== VK_LMENU
|| vk
== VK_RMENU
) {
123 if ((QueueKeyStateTable
[VK_LMENU
] & KS_DOWN_BIT
) ||
124 (QueueKeyStateTable
[VK_RMENU
] & KS_DOWN_BIT
)) {
125 QueueKeyStateTable
[VK_MENU
] |= KS_DOWN_BIT
;
127 QueueKeyStateTable
[VK_MENU
] &= ~KS_DOWN_MASK
;
132 VOID
DumpKeyState( PBYTE KeyState
) {
135 DbgPrint( "KeyState { " );
136 for( i
= 0; i
< 0x100; i
++ ) {
137 if( KeyState
[i
] ) DbgPrint( "%02x(%02x) ", i
, KeyState
[i
] );
142 static BYTE
KeysSet( PKBDTABLES pkKT
, PBYTE KeyState
,
143 int FakeModLeft
, int FakeModRight
) {
144 if( !KeyState
|| !pkKT
) return 0;
146 /* Search special codes first */
147 if( FakeModLeft
&& KeyState
[FakeModLeft
] )
148 return KeyState
[FakeModLeft
];
149 else if( FakeModRight
&& KeyState
[FakeModRight
] )
150 return KeyState
[FakeModRight
];
155 /* Search the keyboard layout modifiers table for the shift bit. I don't
156 * want to count on the shift bit not moving, because it can be specified
159 static DWORD FASTCALL
GetShiftBit( PKBDTABLES pkKT
, DWORD Vk
) {
162 for( i
= 0; pkKT
->pCharModifiers
->pVkToBit
[i
].Vk
; i
++ )
163 if( pkKT
->pCharModifiers
->pVkToBit
[i
].Vk
== Vk
)
164 return pkKT
->pCharModifiers
->pVkToBit
[i
].ModBits
;
169 static DWORD
ModBits( PKBDTABLES pkKT
, PBYTE KeyState
) {
172 if( !KeyState
) return 0;
174 /* DumpKeyState( KeyState ); */
176 if (KeysSet( pkKT
, KeyState
, VK_LSHIFT
, VK_RSHIFT
) &
178 ModBits
|= GetShiftBit( pkKT
, VK_SHIFT
);
180 if (KeysSet( pkKT
, KeyState
, VK_LCONTROL
, VK_RCONTROL
) &
182 ModBits
|= GetShiftBit( pkKT
, VK_CONTROL
);
184 if (KeysSet( pkKT
, KeyState
, VK_LMENU
, VK_RMENU
) &
186 ModBits
|= GetShiftBit( pkKT
, VK_MENU
);
189 if (KeysSet( pkKT
, KeyState
, VK_RMENU
, 0 ) &
191 ModBits
|= GetShiftBit( pkKT
, VK_CONTROL
);
193 /* Deal with VK_CAPITAL */
194 if (KeysSet( pkKT
, KeyState
, VK_CAPITAL
, 0 ) & KS_LOCK_BIT
)
196 ModBits
|= CAPITAL_BIT
;
199 /* Deal with VK_NUMLOCK */
200 if (KeysSet( pkKT
, KeyState
, VK_NUMLOCK
, 0 ) & KS_LOCK_BIT
)
202 ModBits
|= NUMLOCK_BIT
;
205 DPRINT( "Current Mod Bits: %x\n", ModBits
);
210 static BOOL
TryToTranslateChar(WORD wVirtKey
,
214 PWCHAR pwcTranslatedChar
,
215 PKBDTABLES keyLayout
)
217 PVK_TO_WCHAR_TABLE vtwTbl
;
218 PVK_TO_WCHARS10 vkPtr
;
219 size_t size_this_entry
;
221 DWORD CapsMod
= 0, CapsState
= 0;
223 CapsState
= ModBits
& ~MOD_BITS_MASK
;
224 ModBits
= ModBits
& MOD_BITS_MASK
;
226 DPRINT ( "TryToTranslate: %04x %x\n", wVirtKey
, ModBits
);
228 if (ModBits
> keyLayout
->pCharModifiers
->wMaxModBits
)
232 shift
= keyLayout
->pCharModifiers
->ModNumber
[ModBits
];
234 for (nMod
= 0; keyLayout
->pVkToWcharTable
[nMod
].nModifications
; nMod
++)
236 vtwTbl
= &keyLayout
->pVkToWcharTable
[nMod
];
237 size_this_entry
= vtwTbl
->cbSize
;
238 vkPtr
= (PVK_TO_WCHARS10
)((BYTE
*)vtwTbl
->pVkToWchars
);
239 while(vkPtr
->VirtualKey
)
241 if( wVirtKey
== (vkPtr
->VirtualKey
& 0xff) )
244 shift
| ((CapsState
& CAPITAL_BIT
) ? vkPtr
->Attributes
: 0);
246 if( CapsMod
> keyLayout
->pVkToWcharTable
[nMod
].nModifications
) {
249 keyLayout
->pVkToWcharTable
[nMod
].nModifications
)
252 CapsMod
&= MaxBit
- 1; /* Guarantee that CapsMod lies
256 *pbDead
= vkPtr
->wch
[CapsMod
] == WCH_DEAD
;
257 *pbLigature
= vkPtr
->wch
[CapsMod
] == WCH_LGTR
;
258 *pwcTranslatedChar
= vkPtr
->wch
[CapsMod
];
260 DPRINT("%d %04x: CapsMod %08x CapsState %08x shift %08x Char %04x\n",
262 CapsMod
, CapsState
, shift
, *pwcTranslatedChar
);
266 vkPtr
= (PVK_TO_WCHARS10
)(((BYTE
*)vkPtr
) + size_this_entry
);
267 if( vkPtr
->VirtualKey
!= 0xff )
269 DPRINT( "Found dead key with no trailer in the table.\n" );
270 DPRINT( "VK: %04x, ADDR: %08x\n", wVirtKey
, (int)vkPtr
);
273 *pwcTranslatedChar
= vkPtr
->wch
[shift
];
277 vkPtr
= (PVK_TO_WCHARS10
)(((BYTE
*)vkPtr
) + size_this_entry
);
285 ToUnicodeInner(UINT wVirtKey
,
293 WCHAR wcTranslatedChar
;
297 if( !pkKT
) return 0;
299 if( TryToTranslateChar( wVirtKey
,
300 ModBits( pkKT
, lpKeyState
),
308 DPRINT("Not handling ligature (yet)\n" );
312 if( cchBuff
> 0 ) pwszBuff
[0] = wcTranslatedChar
;
314 return bDead
? -1 : 1;
329 ret
= ((DWORD
)(QueueKeyStateTable
[key
] & KS_DOWN_BIT
) << 8 ) |
330 (QueueKeyStateTable
[key
] & KS_LOCK_BIT
);
336 int STDCALL
ToUnicodeEx( UINT wVirtKey
,
343 int ToUnicodeResult
= 0;
345 if (0 == (lpKeyState
[wVirtKey
] & KS_DOWN_BIT
))
352 ToUnicodeResult
= ToUnicodeInner( wVirtKey
,
359 PsGetWin32Thread()->KeyboardLayout
: 0 );
363 return ToUnicodeResult
;
366 int STDCALL
ToUnicode( UINT wVirtKey
,
372 return ToUnicodeEx( wVirtKey
,
382 * Utility to copy and append two unicode strings.
384 * IN OUT PUNICODE_STRING ResultFirst -> First string and result
385 * IN PUNICODE_STRING Second -> Second string to append
386 * IN BOOL Deallocate -> TRUE: Deallocate First string before
392 NTSTATUS NTAPI
AppendUnicodeString(PUNICODE_STRING ResultFirst
,
393 PUNICODE_STRING Second
,
397 ExAllocatePoolWithTag(PagedPool
,
398 (ResultFirst
->Length
+ Second
->Length
+ sizeof(WCHAR
)),
401 return STATUS_NO_MEMORY
;
403 memcpy( new_string
, ResultFirst
->Buffer
,
404 ResultFirst
->Length
);
405 memcpy( new_string
+ ResultFirst
->Length
/ sizeof(WCHAR
),
408 if( Deallocate
) RtlFreeUnicodeString(ResultFirst
);
409 ResultFirst
->Length
+= Second
->Length
;
410 ResultFirst
->MaximumLength
= ResultFirst
->Length
;
411 new_string
[ResultFirst
->Length
/ sizeof(WCHAR
)] = 0;
412 Status
= RtlCreateUnicodeString(ResultFirst
,new_string
) ?
413 STATUS_SUCCESS
: STATUS_NO_MEMORY
;
414 ExFreePool(new_string
);
419 * Utility function to read a value from the registry more easily.
421 * IN PUNICODE_STRING KeyName -> Name of key to open
422 * IN PUNICODE_STRING ValueName -> Name of value to open
423 * OUT PUNICODE_STRING ReturnedValue -> String contained in registry
428 static NTSTATUS NTAPI
ReadRegistryValue( PUNICODE_STRING KeyName
,
429 PUNICODE_STRING ValueName
,
430 PUNICODE_STRING ReturnedValue
) {
433 OBJECT_ATTRIBUTES KeyAttributes
;
434 PKEY_VALUE_PARTIAL_INFORMATION KeyValuePartialInfo
;
439 InitializeObjectAttributes(&KeyAttributes
, KeyName
, OBJ_CASE_INSENSITIVE
,
441 Status
= ZwOpenKey(&KeyHandle
, KEY_ALL_ACCESS
, &KeyAttributes
);
442 if( !NT_SUCCESS(Status
) ) {
446 Status
= ZwQueryValueKey(KeyHandle
, ValueName
, KeyValuePartialInformation
,
451 if( Status
!= STATUS_BUFFER_TOO_SMALL
) {
456 ResLength
+= sizeof( *KeyValuePartialInfo
);
457 KeyValuePartialInfo
=
458 ExAllocatePoolWithTag(PagedPool
, ResLength
, TAG_STRING
);
461 if( !KeyValuePartialInfo
) {
463 return STATUS_NO_MEMORY
;
466 Status
= ZwQueryValueKey(KeyHandle
, ValueName
, KeyValuePartialInformation
,
467 (PVOID
)KeyValuePartialInfo
,
471 if( !NT_SUCCESS(Status
) ) {
473 ExFreePool(KeyValuePartialInfo
);
477 Temp
.Length
= Temp
.MaximumLength
= KeyValuePartialInfo
->DataLength
;
478 Temp
.Buffer
= (PWCHAR
)KeyValuePartialInfo
->Data
;
480 /* At this point, KeyValuePartialInfo->Data contains the key data */
481 RtlInitUnicodeString(ReturnedValue
,L
"");
482 AppendUnicodeString(ReturnedValue
,&Temp
,FALSE
);
484 ExFreePool(KeyValuePartialInfo
);
490 typedef PVOID (*KbdLayerDescriptor
)(VOID
);
491 NTSTATUS STDCALL
LdrGetProcedureAddress(PVOID module
,
492 PANSI_STRING import_name
,
496 void InitKbdLayout( PVOID
*pkKeyboardLayout
)
498 WCHAR LocaleBuffer
[16];
499 UNICODE_STRING LayoutKeyName
;
500 UNICODE_STRING LayoutValueName
;
501 UNICODE_STRING DefaultLocale
;
502 UNICODE_STRING LayoutFile
;
503 UNICODE_STRING FullLayoutPath
;
505 PWCHAR KeyboardLayoutWSTR
;
506 HMODULE kbModule
= 0;
508 ANSI_STRING kbdProcedureName
;
509 KbdLayerDescriptor layerDescGetFn
;
511 #define XX_STATUS(x) if (!NT_SUCCESS(Status = (x))) continue;
514 Status
= ZwQueryDefaultLocale(FALSE
, &LocaleId
);
515 if (!NT_SUCCESS(Status
))
517 DPRINT1("Could not get default locale (%08lx).\n", Status
);
521 DPRINT("DefaultLocale = %lx\n", LocaleId
);
522 swprintf(LocaleBuffer
, L
"%08lx", LocaleId
);
523 DPRINT("DefaultLocale = %S\n", LocaleBuffer
);
524 RtlInitUnicodeString(&DefaultLocale
, LocaleBuffer
);
526 RtlInitUnicodeString(&LayoutKeyName
,
527 L
"\\REGISTRY\\Machine\\SYSTEM\\CurrentControlSet"
528 L
"\\Control\\KeyboardLayouts\\");
530 AppendUnicodeString(&LayoutKeyName
,&DefaultLocale
,FALSE
);
532 RtlInitUnicodeString(&LayoutValueName
,L
"Layout File");
534 Status
= ReadRegistryValue(&LayoutKeyName
,&LayoutValueName
,&LayoutFile
);
535 RtlInitUnicodeString(&FullLayoutPath
,SYSTEMROOT_DIR
);
537 if( !NT_SUCCESS(Status
) ) {
538 DPRINT1("Got default locale but not layout file. (%08x)\n",
540 RtlFreeUnicodeString(&LayoutFile
);
542 DPRINT("Read registry and got %wZ\n", &LayoutFile
);
544 RtlFreeUnicodeString(&LayoutKeyName
);
546 AppendUnicodeString(&FullLayoutPath
,&LayoutFile
,FALSE
);
548 DPRINT("Loading Keyboard DLL %wZ\n", &FullLayoutPath
);
550 RtlFreeUnicodeString(&LayoutFile
);
553 ExAllocatePoolWithTag(PagedPool
,
554 FullLayoutPath
.Length
+ sizeof(WCHAR
),
557 if( !KeyboardLayoutWSTR
) {
558 DPRINT1("Couldn't allocate a string for the keyboard layout name.\n");
559 RtlFreeUnicodeString(&FullLayoutPath
);
562 memcpy(KeyboardLayoutWSTR
,FullLayoutPath
.Buffer
,
563 FullLayoutPath
.Length
+ sizeof(WCHAR
));
564 KeyboardLayoutWSTR
[FullLayoutPath
.Length
/ sizeof(WCHAR
)] = 0;
566 kbModule
= EngLoadImage(KeyboardLayoutWSTR
);
567 DPRINT( "Load Keyboard Layout: %S\n", KeyboardLayoutWSTR
);
570 DPRINT1( "Load Keyboard Layout: No %wZ\n", &FullLayoutPath
);
573 RtlFreeUnicodeString(&FullLayoutPath
);
578 DPRINT1("Trying to load US Keyboard Layout\n");
579 kbModule
= EngLoadImage(L
"\\SystemRoot\\system32\\kbdus.dll");
583 DPRINT1("Failed to load any Keyboard Layout\n");
588 RtlInitAnsiString( &kbdProcedureName
, "KbdLayerDescriptor" );
590 LdrGetProcedureAddress((PVOID
)kbModule
,
593 (PVOID
*)&layerDescGetFn
);
595 if( layerDescGetFn
) {
596 *pkKeyboardLayout
= layerDescGetFn();
600 if( !*pkKeyboardLayout
) {
601 DPRINT1("Failed to load the keyboard layout.\n");
607 PKBDTABLES
W32kGetDefaultKeyLayout() {
608 PKBDTABLES pkKeyboardLayout
= 0;
609 InitKbdLayout( (PVOID
) &pkKeyboardLayout
);
610 return pkKeyboardLayout
;
614 IntTranslateKbdMessage(LPMSG lpMsg
,
617 static INT dead_char
= 0;
621 PKBDTABLES keyLayout
;
626 keyLayout
= PsGetWin32Thread()->KeyboardLayout
;
630 if (lpMsg
->message
!= WM_KEYDOWN
&& lpMsg
->message
!= WM_SYSKEYDOWN
)
633 ScanCode
= (lpMsg
->lParam
>> 16) & 0xff;
637 UState
= ToUnicodeInner(lpMsg
->wParam
, HIWORD(lpMsg
->lParam
) & 0xff,
638 QueueKeyStateTable
, wp
, 2, 0,
643 NewMsg
.message
= (lpMsg
->message
== WM_KEYDOWN
) ? WM_CHAR
: WM_SYSCHAR
;
648 DPRINT("PREVIOUS DEAD CHAR: %c\n", dead_char
);
650 for( i
= 0; keyLayout
->pDeadKey
[i
].dwBoth
; i
++ )
652 first
= keyLayout
->pDeadKey
[i
].dwBoth
>> 16;
653 second
= keyLayout
->pDeadKey
[i
].dwBoth
;
654 if (first
== dead_char
&& second
== wp
[0])
656 wp
[0] = keyLayout
->pDeadKey
[i
].wchComposed
;
662 DPRINT("FINAL CHAR: %c\n", wp
[0]);
666 NewMsg
.hwnd
= lpMsg
->hwnd
;
667 NewMsg
.wParam
= dead_char
;
668 NewMsg
.lParam
= lpMsg
->lParam
;
670 MsqPostMessage(PsGetWin32Thread()->MessageQueue
, &NewMsg
, FALSE
, QS_KEY
);
673 NewMsg
.hwnd
= lpMsg
->hwnd
;
674 NewMsg
.wParam
= wp
[0];
675 NewMsg
.lParam
= lpMsg
->lParam
;
676 DPRINT( "CHAR='%c' %04x %08x\n", wp
[0], wp
[0], lpMsg
->lParam
);
677 MsqPostMessage(PsGetWin32Thread()->MessageQueue
, &NewMsg
, FALSE
, QS_KEY
);
680 else if (UState
== -1)
683 (lpMsg
->message
== WM_KEYDOWN
) ? WM_DEADCHAR
: WM_SYSDEADCHAR
;
684 NewMsg
.hwnd
= lpMsg
->hwnd
;
685 NewMsg
.wParam
= wp
[0];
686 NewMsg
.lParam
= lpMsg
->lParam
;
688 MsqPostMessage(PsGetWin32Thread()->MessageQueue
, &NewMsg
, FALSE
, QS_KEY
);
698 NtUserGetKeyboardState(
705 if(!NT_SUCCESS(MmCopyToCaller(lpKeyState
, QueueKeyStateTable
, 256)))
714 NtUserSetKeyboardState(
721 if(! NT_SUCCESS(MmCopyFromCaller(QueueKeyStateTable
, lpKeyState
, 256)))
729 static UINT
VkToScan( UINT Code
, BOOL ExtCode
, PKBDTABLES pkKT
) {
732 for( i
= 0; i
< pkKT
->bMaxVSCtoVK
; i
++ ) {
733 if( pkKT
->pusVSCtoVK
[i
] == Code
) { return i
; }
739 UINT
ScanToVk( UINT Code
, BOOL ExtKey
, PKBDTABLES pkKT
) {
741 DPRINT("ScanToVk: No layout\n");
748 for( i
= 0; pkKT
->pVSCtoVK_E0
[i
].Vsc
; i
++ ) {
749 if( pkKT
->pVSCtoVK_E0
[i
].Vsc
== Code
)
750 return pkKT
->pVSCtoVK_E0
[i
].Vk
& 0xff;
752 for( i
= 0; pkKT
->pVSCtoVK_E1
[i
].Vsc
; i
++ ) {
753 if( pkKT
->pVSCtoVK_E1
[i
].Vsc
== Code
)
754 return pkKT
->pVSCtoVK_E1
[i
].Vk
& 0xff;
759 if( Code
>= pkKT
->bMaxVSCtoVK
) { return 0; }
760 return pkKT
->pusVSCtoVK
[Code
] & 0xff;
765 * Map a virtual key code, or virtual scan code, to a scan code, key code,
766 * or unshifted unicode character.
770 * 0 -- Code is a virtual key code that is converted into a virtual scan code
771 * that does not distinguish between left and right shift keys.
772 * 1 -- Code is a virtual scan code that is converted into a virtual key code
773 * that does not distinguish between left and right shift keys.
774 * 2 -- Code is a virtual key code that is converted into an unshifted unicode
776 * 3 -- Code is a virtual scan code that is converted into a virtual key code
777 * that distinguishes left and right shift keys.
778 * KeyLayout: Keyboard layout handle (currently, unused)
783 static UINT
IntMapVirtualKeyEx( UINT Code
, UINT Type
, PKBDTABLES keyLayout
) {
788 if( Code
== VK_RSHIFT
) Code
= VK_LSHIFT
;
789 if( Code
== VK_RMENU
) Code
= VK_LMENU
;
790 if( Code
== VK_RCONTROL
) Code
= VK_LCONTROL
;
791 ret
= VkToScan( Code
, FALSE
, keyLayout
);
796 DontDistinguishShifts
797 (IntMapVirtualKeyEx( Code
, 3, keyLayout
) );
803 ret
= VkToScan( Code
, FALSE
, keyLayout
);
804 ToUnicodeInner( Code
, ret
, 0, wp
, 2, 0, keyLayout
);
810 ret
= ScanToVk( Code
, FALSE
, keyLayout
);
819 NtUserMapVirtualKeyEx( UINT Code
, UINT Type
, DWORD keyboardId
, HKL dwhkl
) {
820 PKBDTABLES keyLayout
= PsGetWin32Thread() ?
821 PsGetWin32Thread()->KeyboardLayout
: 0;
823 if( !keyLayout
) return 0;
825 return IntMapVirtualKeyEx( Code
, Type
, keyLayout
);
839 BYTE KeyStateBuf
[0x100];
840 PWCHAR OutPwszBuff
= 0;
844 if( !NT_SUCCESS(MmCopyFromCaller(KeyStateBuf
,
846 sizeof(KeyStateBuf
))) ) {
847 DPRINT1( "Couldn't copy key state from caller.\n" );
850 OutPwszBuff
= ExAllocatePoolWithTag(NonPagedPool
,sizeof(WCHAR
) * cchBuff
, TAG_STRING
);
852 DPRINT1( "ExAllocatePool(%d) failed\n", sizeof(WCHAR
) * cchBuff
);
855 RtlZeroMemory( OutPwszBuff
, sizeof( WCHAR
) * cchBuff
);
857 ret
= ToUnicodeEx( wVirtKey
,
865 MmCopyToCaller(pwszBuff
,OutPwszBuff
,sizeof(WCHAR
)*cchBuff
);
866 ExFreePool(OutPwszBuff
);
871 static int W32kSimpleToupper( int ch
) {
872 if( ch
>= 'a' && ch
<= 'z' ) ch
= ch
- 'a' + 'A';
878 NtUserGetKeyNameText( LONG lParam
, LPWSTR lpString
, int nSize
) {
883 UINT ScanCode
= (lParam
>> 16) & 0xff;
884 BOOL ExtKey
= lParam
& (1<<24) ? TRUE
: FALSE
;
885 PKBDTABLES keyLayout
=
887 PsGetWin32Thread()->KeyboardLayout
: 0;
889 if( !keyLayout
|| nSize
< 1 ) return 0;
891 if( lParam
& (1<<25) ) {
892 CareVk
= VkCode
= ScanToVk( ScanCode
, ExtKey
, keyLayout
);
893 if( VkCode
== VK_LSHIFT
|| VkCode
== VK_RSHIFT
)
895 if( VkCode
== VK_LCONTROL
|| VkCode
== VK_RCONTROL
)
896 VkCode
= VK_LCONTROL
;
897 if( VkCode
== VK_LMENU
|| VkCode
== VK_RMENU
)
900 VkCode
= ScanToVk( ScanCode
, ExtKey
, keyLayout
);
903 VSC_LPWSTR
*KeyNames
= 0;
905 if( CareVk
!= VkCode
)
906 ScanCode
= VkToScan( VkCode
, ExtKey
, keyLayout
);
909 KeyNames
= keyLayout
->pKeyNamesExt
;
911 KeyNames
= keyLayout
->pKeyNames
;
913 for( i
= 0; KeyNames
[i
].pwsz
; i
++ ) {
914 if( KeyNames
[i
].vsc
== ScanCode
) {
915 UINT StrLen
= wcslen(KeyNames
[i
].pwsz
);
916 UINT StrMax
= StrLen
> (nSize
- 1) ? (nSize
- 1) : StrLen
;
918 if( NT_SUCCESS( MmCopyToCaller( lpString
,
920 StrMax
* sizeof(WCHAR
) ) ) &&
921 NT_SUCCESS( MmCopyToCaller( lpString
+ StrMax
,
923 sizeof( WCHAR
) ) ) ) {
933 UCName
[0] = W32kSimpleToupper(IntMapVirtualKeyEx( VkCode
, 2, keyLayout
));
937 if( !NT_SUCCESS(MmCopyToCaller( lpString
, UCName
, 2 * sizeof(WCHAR
) )) )
945 * Filter this message according to the current key layout, setting wParam
949 VOID FASTCALL
W32kKeyProcessMessage(LPMSG Msg
, PKBDTABLES KeyboardLayout
) {
950 DWORD ScanCode
= 0, ModifierBits
= 0;
952 DWORD BaseMapping
= 0;
954 static WORD NumpadConversion
[][2] =
955 { { VK_DELETE
, VK_DECIMAL
},
956 { VK_INSERT
, VK_NUMPAD0
},
957 { VK_END
, VK_NUMPAD1
},
958 { VK_DOWN
, VK_NUMPAD2
},
959 { VK_NEXT
, VK_NUMPAD3
},
960 { VK_LEFT
, VK_NUMPAD4
},
961 { VK_CLEAR
, VK_NUMPAD5
},
962 { VK_RIGHT
, VK_NUMPAD6
},
963 { VK_HOME
, VK_NUMPAD7
},
964 { VK_UP
, VK_NUMPAD8
},
965 { VK_PRIOR
, VK_NUMPAD9
},
968 if( !KeyboardLayout
|| !Msg
||
969 (Msg
->message
!= WM_KEYDOWN
&& Msg
->message
!= WM_SYSKEYDOWN
&&
970 Msg
->message
!= WM_KEYUP
&& Msg
->message
!= WM_SYSKEYUP
) )
977 /* arty -- handle numpad -- On real windows, the actual key produced
978 * by the messaging layer is different based on the state of numlock. */
979 ModifierBits
= ModBits(KeyboardLayout
,QueueKeyStateTable
);
981 /* Get the raw scan code, so we can look up whether the key is a numpad
984 * Shift and the LP_EXT_BIT cancel. */
985 ScanCode
= (Msg
->lParam
>> 16) & 0xff;
986 BaseMapping
= Msg
->wParam
=
987 IntMapVirtualKeyEx( ScanCode
, 1, KeyboardLayout
);
988 if( ScanCode
>= KeyboardLayout
->bMaxVSCtoVK
)
991 RawVk
= KeyboardLayout
->pusVSCtoVK
[ScanCode
];
993 if ((ModifierBits
& NUMLOCK_BIT
) &&
994 !(ModifierBits
& GetShiftBit(KeyboardLayout
, VK_SHIFT
)) &&
996 !(Msg
->lParam
& LP_EXT_BIT
))
998 /* The key in question is a numpad key. Search for a translation. */
999 for (i
= 0; NumpadConversion
[i
][0]; i
++)
1001 if ((BaseMapping
& 0xff) == NumpadConversion
[i
][0]) /* RawVk? */
1003 Msg
->wParam
= NumpadConversion
[i
][1];
1009 DPRINT("Key: [%04x -> %04x]\n", BaseMapping
, Msg
->wParam
);
1011 /* Now that we have the VK, we can set the keymap appropriately
1012 * This is a better place for this code, as it's guaranteed to be
1013 * run, unlike translate message. */
1014 if (Msg
->message
== WM_KEYDOWN
|| Msg
->message
== WM_SYSKEYDOWN
)
1016 SetKeyState( ScanCode
, Msg
->wParam
, Msg
->lParam
& LP_EXT_BIT
,
1017 TRUE
); /* Strike key */
1019 else if (Msg
->message
== WM_KEYUP
|| Msg
->message
== WM_SYSKEYUP
)
1021 SetKeyState( ScanCode
, Msg
->wParam
, Msg
->lParam
& LP_EXT_BIT
,
1022 FALSE
); /* Release key */
1025 /* We need to unset SYSKEYDOWN if the ALT key is an ALT+Gr */
1026 if( QueueKeyStateTable
[VK_RMENU
] & KS_DOWN_BIT
) {
1027 if( Msg
->message
== WM_SYSKEYDOWN
) Msg
->message
= WM_KEYDOWN
;
1028 else Msg
->message
= WM_KEYUP
;
1031 IntUnLockQueueState
;