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.
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS kernel
23 * FILE: subsys/win32k/ntuser/keyboard.c
24 * PROGRAMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
26 * 06-06-2001 CSH Created
29 /* INCLUDES ******************************************************************/
36 /* Directory to load key layouts from */
37 #define SYSTEMROOT_DIR L"\\SystemRoot\\System32\\"
39 #define CAPITAL_BIT 0x80000000
40 #define NUMLOCK_BIT 0x40000000
41 #define MOD_BITS_MASK 0x3fffffff
42 #define MOD_KCTRL 0x02
44 #define KS_DOWN_MASK 0xc0
45 #define KS_DOWN_BIT 0x80
46 #define KS_LOCK_BIT 0x01
48 #define LP_EXT_BIT (1<<24)
49 /* From kbdxx.c -- Key changes with numlock */
52 /* Lock the keyboard state to prevent unusual concurrent access */
53 FAST_MUTEX QueueStateLock
;
55 BYTE QueueKeyStateTable
[256];
57 #define IntLockQueueState \
58 ExAcquireFastMutex(&QueueStateLock)
60 #define IntUnLockQueueState \
61 ExReleaseFastMutex(&QueueStateLock)
63 /* FUNCTIONS *****************************************************************/
65 /* Initialization -- Right now, just zero the key state and init the lock */
66 NTSTATUS FASTCALL
InitKeyboardImpl(VOID
) {
67 ExInitializeFastMutex(&QueueStateLock
);
68 RtlZeroMemory(&QueueKeyStateTable
,0x100);
69 return STATUS_SUCCESS
;
72 /*** Statics used by TranslateMessage ***/
74 /*** Shift state code was out of hand, sorry. --- arty */
76 static UINT
DontDistinguishShifts( UINT ret
) {
77 if( ret
== VK_LSHIFT
|| ret
== VK_RSHIFT
) ret
= VK_LSHIFT
;
78 if( ret
== VK_LCONTROL
|| ret
== VK_RCONTROL
) ret
= VK_LCONTROL
;
79 if( ret
== VK_LMENU
|| ret
== VK_RMENU
) ret
= VK_LMENU
;
83 static VOID STDCALL
SetKeyState(DWORD key
, DWORD vk
, DWORD ext
, BOOL down
) {
86 /* Special handling for toggles like numpad and caps lock */
87 if (vk
== VK_CAPITAL
|| vk
== VK_NUMLOCK
) {
88 if (down
) QueueKeyStateTable
[vk
] ^= KS_LOCK_BIT
;
91 if (ext
&& vk
== VK_LSHIFT
)
93 if (ext
&& vk
== VK_LCONTROL
)
95 if (ext
&& vk
== VK_LMENU
)
99 QueueKeyStateTable
[vk
] |= KS_DOWN_BIT
;
101 QueueKeyStateTable
[vk
] &= ~KS_DOWN_MASK
;
103 if (vk
== VK_LSHIFT
|| vk
== VK_RSHIFT
) {
104 if ((QueueKeyStateTable
[VK_LSHIFT
] & KS_DOWN_BIT
) ||
105 (QueueKeyStateTable
[VK_RSHIFT
] & KS_DOWN_BIT
)) {
106 QueueKeyStateTable
[VK_SHIFT
] |= KS_DOWN_BIT
;
108 QueueKeyStateTable
[VK_SHIFT
] &= ~KS_DOWN_MASK
;
112 if (vk
== VK_LCONTROL
|| vk
== VK_RCONTROL
) {
113 if ((QueueKeyStateTable
[VK_LCONTROL
] & KS_DOWN_BIT
) ||
114 (QueueKeyStateTable
[VK_RCONTROL
] & KS_DOWN_BIT
)) {
115 QueueKeyStateTable
[VK_CONTROL
] |= KS_DOWN_BIT
;
117 QueueKeyStateTable
[VK_CONTROL
] &= ~KS_DOWN_MASK
;
121 if (vk
== VK_LMENU
|| vk
== VK_RMENU
) {
122 if ((QueueKeyStateTable
[VK_LMENU
] & KS_DOWN_BIT
) ||
123 (QueueKeyStateTable
[VK_RMENU
] & KS_DOWN_BIT
)) {
124 QueueKeyStateTable
[VK_MENU
] |= KS_DOWN_BIT
;
126 QueueKeyStateTable
[VK_MENU
] &= ~KS_DOWN_MASK
;
131 VOID
DumpKeyState( PBYTE KeyState
) {
134 DbgPrint( "KeyState { " );
135 for( i
= 0; i
< 0x100; i
++ ) {
136 if( KeyState
[i
] ) DbgPrint( "%02x(%02x) ", i
, KeyState
[i
] );
141 static BYTE
KeysSet( PKBDTABLES pkKT
, PBYTE KeyState
,
142 int FakeModLeft
, int FakeModRight
) {
143 if( !KeyState
|| !pkKT
) return 0;
145 /* Search special codes first */
146 if( FakeModLeft
&& KeyState
[FakeModLeft
] )
147 return KeyState
[FakeModLeft
];
148 else if( FakeModRight
&& KeyState
[FakeModRight
] )
149 return KeyState
[FakeModRight
];
154 /* Search the keyboard layout modifiers table for the shift bit. I don't
155 * want to count on the shift bit not moving, because it can be specified
158 static DWORD FASTCALL
GetShiftBit( PKBDTABLES pkKT
, DWORD Vk
) {
161 for( i
= 0; pkKT
->pCharModifiers
->pVkToBit
[i
].Vk
; i
++ )
162 if( pkKT
->pCharModifiers
->pVkToBit
[i
].Vk
== Vk
)
163 return pkKT
->pCharModifiers
->pVkToBit
[i
].ModBits
;
168 static DWORD
ModBits( PKBDTABLES pkKT
, PBYTE KeyState
) {
171 if( !KeyState
) return 0;
173 /* DumpKeyState( KeyState ); */
175 if (KeysSet( pkKT
, KeyState
, VK_LSHIFT
, VK_RSHIFT
) &
177 ModBits
|= GetShiftBit( pkKT
, VK_SHIFT
);
179 if (KeysSet( pkKT
, KeyState
, VK_LCONTROL
, VK_RCONTROL
) &
181 ModBits
|= GetShiftBit( pkKT
, VK_CONTROL
);
183 if (KeysSet( pkKT
, KeyState
, VK_LMENU
, VK_RMENU
) &
185 ModBits
|= GetShiftBit( pkKT
, VK_MENU
);
188 if (KeysSet( pkKT
, KeyState
, VK_RMENU
, 0 ) &
190 ModBits
|= GetShiftBit( pkKT
, VK_CONTROL
);
192 /* Deal with VK_CAPITAL */
193 if (KeysSet( pkKT
, KeyState
, VK_CAPITAL
, 0 ) & KS_LOCK_BIT
)
195 ModBits
|= CAPITAL_BIT
;
198 /* Deal with VK_NUMLOCK */
199 if (KeysSet( pkKT
, KeyState
, VK_NUMLOCK
, 0 ) & KS_LOCK_BIT
)
201 ModBits
|= NUMLOCK_BIT
;
204 DPRINT( "Current Mod Bits: %x\n", ModBits
);
209 static BOOL
TryToTranslateChar(WORD wVirtKey
,
213 PWCHAR pwcTranslatedChar
,
214 PKBDTABLES keyLayout
)
216 PVK_TO_WCHAR_TABLE vtwTbl
;
217 PVK_TO_WCHARS10 vkPtr
;
218 size_t size_this_entry
;
220 DWORD CapsMod
= 0, CapsState
= 0;
222 CapsState
= ModBits
& ~MOD_BITS_MASK
;
223 ModBits
= ModBits
& MOD_BITS_MASK
;
225 DPRINT ( "TryToTranslate: %04x %x\n", wVirtKey
, ModBits
);
227 if (ModBits
> keyLayout
->pCharModifiers
->wMaxModBits
)
231 for (nMod
= 0; keyLayout
->pVkToWcharTable
[nMod
].nModifications
; nMod
++)
233 vtwTbl
= &keyLayout
->pVkToWcharTable
[nMod
];
234 size_this_entry
= vtwTbl
->cbSize
;
235 vkPtr
= (PVK_TO_WCHARS10
)((BYTE
*)vtwTbl
->pVkToWchars
);
236 while(vkPtr
->VirtualKey
)
238 if( wVirtKey
== (vkPtr
->VirtualKey
& 0xff) )
240 CapsMod
= keyLayout
->pCharModifiers
->ModNumber
242 ((CapsState
& CAPITAL_BIT
) ? vkPtr
->Attributes
: 0)];
244 if( CapsMod
> keyLayout
->pVkToWcharTable
[nMod
].nModifications
) {
247 keyLayout
->pVkToWcharTable
[nMod
].nModifications
)
250 CapsMod
&= MaxBit
- 1; /* Guarantee that CapsMod lies
254 *pbDead
= vkPtr
->wch
[CapsMod
] == WCH_DEAD
;
255 *pbLigature
= vkPtr
->wch
[CapsMod
] == WCH_LGTR
;
256 *pwcTranslatedChar
= vkPtr
->wch
[CapsMod
];
258 DPRINT("%d %04x: CapsMod %08x CapsState %08x Char %04x\n",
260 CapsMod
, CapsState
, *pwcTranslatedChar
);
264 vkPtr
= (PVK_TO_WCHARS10
)(((BYTE
*)vkPtr
) + size_this_entry
);
265 if( vkPtr
->VirtualKey
!= 0xff )
267 DPRINT( "Found dead key with no trailer in the table.\n" );
268 DPRINT( "VK: %04x, ADDR: %08x\n", wVirtKey
, (int)vkPtr
);
271 *pwcTranslatedChar
= vkPtr
->wch
[CapsMod
];
275 vkPtr
= (PVK_TO_WCHARS10
)(((BYTE
*)vkPtr
) + size_this_entry
);
283 ToUnicodeInner(UINT wVirtKey
,
291 WCHAR wcTranslatedChar
;
295 if( !pkKT
) return 0;
297 if( TryToTranslateChar( wVirtKey
,
298 ModBits( pkKT
, lpKeyState
),
306 DPRINT("Not handling ligature (yet)\n" );
310 if( cchBuff
> 0 ) pwszBuff
[0] = wcTranslatedChar
;
312 return bDead
? -1 : 1;
327 ret
= ((DWORD
)(QueueKeyStateTable
[key
] & KS_DOWN_BIT
) << 8 ) |
328 (QueueKeyStateTable
[key
] & KS_LOCK_BIT
);
336 NtUserGetAsyncKeyState(
343 ret
= ((DWORD
)(QueueKeyStateTable
[key
] & KS_DOWN_BIT
) << 8 ) |
344 (QueueKeyStateTable
[key
] & KS_LOCK_BIT
);
352 int STDCALL
ToUnicodeEx( UINT wVirtKey
,
359 int ToUnicodeResult
= 0;
361 if (0 == (lpKeyState
[wVirtKey
] & KS_DOWN_BIT
))
368 ToUnicodeResult
= ToUnicodeInner( wVirtKey
,
375 PsGetWin32Thread()->KeyboardLayout
: 0 );
379 return ToUnicodeResult
;
382 int STDCALL
ToUnicode( UINT wVirtKey
,
388 return ToUnicodeEx( wVirtKey
,
398 * Utility to copy and append two unicode strings.
400 * IN OUT PUNICODE_STRING ResultFirst -> First string and result
401 * IN PUNICODE_STRING Second -> Second string to append
402 * IN BOOL Deallocate -> TRUE: Deallocate First string before
408 NTSTATUS NTAPI
AppendUnicodeString(PUNICODE_STRING ResultFirst
,
409 PUNICODE_STRING Second
,
413 ExAllocatePoolWithTag(PagedPool
,
414 (ResultFirst
->Length
+ Second
->Length
+ sizeof(WCHAR
)),
417 return STATUS_NO_MEMORY
;
419 memcpy( new_string
, ResultFirst
->Buffer
,
420 ResultFirst
->Length
);
421 memcpy( new_string
+ ResultFirst
->Length
/ sizeof(WCHAR
),
424 if( Deallocate
) RtlFreeUnicodeString(ResultFirst
);
425 ResultFirst
->Length
+= Second
->Length
;
426 ResultFirst
->MaximumLength
= ResultFirst
->Length
;
427 new_string
[ResultFirst
->Length
/ sizeof(WCHAR
)] = 0;
428 Status
= RtlCreateUnicodeString(ResultFirst
,new_string
) ?
429 STATUS_SUCCESS
: STATUS_NO_MEMORY
;
430 ExFreePool(new_string
);
435 * Utility function to read a value from the registry more easily.
437 * IN PUNICODE_STRING KeyName -> Name of key to open
438 * IN PUNICODE_STRING ValueName -> Name of value to open
439 * OUT PUNICODE_STRING ReturnedValue -> String contained in registry
444 static NTSTATUS NTAPI
ReadRegistryValue( PUNICODE_STRING KeyName
,
445 PUNICODE_STRING ValueName
,
446 PUNICODE_STRING ReturnedValue
) {
449 OBJECT_ATTRIBUTES KeyAttributes
;
450 PKEY_VALUE_PARTIAL_INFORMATION KeyValuePartialInfo
;
455 InitializeObjectAttributes(&KeyAttributes
, KeyName
, OBJ_CASE_INSENSITIVE
,
457 Status
= ZwOpenKey(&KeyHandle
, KEY_ALL_ACCESS
, &KeyAttributes
);
458 if( !NT_SUCCESS(Status
) ) {
462 Status
= ZwQueryValueKey(KeyHandle
, ValueName
, KeyValuePartialInformation
,
467 if( Status
!= STATUS_BUFFER_TOO_SMALL
) {
472 ResLength
+= sizeof( *KeyValuePartialInfo
);
473 KeyValuePartialInfo
=
474 ExAllocatePoolWithTag(PagedPool
, ResLength
, TAG_STRING
);
477 if( !KeyValuePartialInfo
) {
479 return STATUS_NO_MEMORY
;
482 Status
= ZwQueryValueKey(KeyHandle
, ValueName
, KeyValuePartialInformation
,
483 (PVOID
)KeyValuePartialInfo
,
487 if( !NT_SUCCESS(Status
) ) {
489 ExFreePool(KeyValuePartialInfo
);
493 Temp
.Length
= Temp
.MaximumLength
= KeyValuePartialInfo
->DataLength
;
494 Temp
.Buffer
= (PWCHAR
)KeyValuePartialInfo
->Data
;
496 /* At this point, KeyValuePartialInfo->Data contains the key data */
497 RtlInitUnicodeString(ReturnedValue
,L
"");
498 AppendUnicodeString(ReturnedValue
,&Temp
,FALSE
);
500 ExFreePool(KeyValuePartialInfo
);
506 typedef PVOID (*KbdLayerDescriptor
)(VOID
);
507 NTSTATUS STDCALL
LdrGetProcedureAddress(PVOID module
,
508 PANSI_STRING import_name
,
512 void InitKbdLayout( PVOID
*pkKeyboardLayout
)
514 WCHAR LocaleBuffer
[16];
515 UNICODE_STRING LayoutKeyName
;
516 UNICODE_STRING LayoutValueName
;
517 UNICODE_STRING DefaultLocale
;
518 UNICODE_STRING LayoutFile
;
519 UNICODE_STRING FullLayoutPath
;
521 PWCHAR KeyboardLayoutWSTR
;
522 HMODULE kbModule
= 0;
524 ANSI_STRING kbdProcedureName
;
525 KbdLayerDescriptor layerDescGetFn
;
527 #define XX_STATUS(x) if (!NT_SUCCESS(Status = (x))) continue;
530 Status
= ZwQueryDefaultLocale(FALSE
, &LocaleId
);
531 if (!NT_SUCCESS(Status
))
533 DPRINT1("Could not get default locale (%08lx).\n", Status
);
537 DPRINT("DefaultLocale = %lx\n", LocaleId
);
538 swprintf(LocaleBuffer
, L
"%08lx", LocaleId
);
539 DPRINT("DefaultLocale = %S\n", LocaleBuffer
);
540 RtlInitUnicodeString(&DefaultLocale
, LocaleBuffer
);
542 RtlInitUnicodeString(&LayoutKeyName
,
543 L
"\\REGISTRY\\Machine\\SYSTEM\\CurrentControlSet"
544 L
"\\Control\\KeyboardLayouts\\");
546 AppendUnicodeString(&LayoutKeyName
,&DefaultLocale
,FALSE
);
548 RtlInitUnicodeString(&LayoutValueName
,L
"Layout File");
550 Status
= ReadRegistryValue(&LayoutKeyName
,&LayoutValueName
,&LayoutFile
);
551 RtlInitUnicodeString(&FullLayoutPath
,SYSTEMROOT_DIR
);
553 if( !NT_SUCCESS(Status
) ) {
554 DPRINT1("Got default locale but not layout file. (%08lx)\n",
557 DPRINT("Read registry and got %wZ\n", &LayoutFile
);
559 RtlFreeUnicodeString(&LayoutKeyName
);
561 AppendUnicodeString(&FullLayoutPath
,&LayoutFile
,FALSE
);
563 DPRINT("Loading Keyboard DLL %wZ\n", &FullLayoutPath
);
565 RtlFreeUnicodeString(&LayoutFile
);
568 ExAllocatePoolWithTag(PagedPool
,
569 FullLayoutPath
.Length
+ sizeof(WCHAR
),
572 if( !KeyboardLayoutWSTR
) {
573 DPRINT1("Couldn't allocate a string for the keyboard layout name.\n");
574 RtlFreeUnicodeString(&FullLayoutPath
);
577 memcpy(KeyboardLayoutWSTR
,FullLayoutPath
.Buffer
,
578 FullLayoutPath
.Length
+ sizeof(WCHAR
));
579 KeyboardLayoutWSTR
[FullLayoutPath
.Length
/ sizeof(WCHAR
)] = 0;
581 kbModule
= EngLoadImage(KeyboardLayoutWSTR
);
582 DPRINT( "Load Keyboard Layout: %S\n", KeyboardLayoutWSTR
);
585 DPRINT1( "Load Keyboard Layout: No %wZ\n", &FullLayoutPath
);
587 RtlFreeUnicodeString(&FullLayoutPath
);
593 DPRINT1("Trying to load US Keyboard Layout\n");
594 kbModule
= EngLoadImage(L
"\\SystemRoot\\system32\\kbdus.dll");
598 DPRINT1("Failed to load any Keyboard Layout\n");
603 RtlInitAnsiString( &kbdProcedureName
, "KbdLayerDescriptor" );
605 LdrGetProcedureAddress((PVOID
)kbModule
,
608 (PVOID
*)&layerDescGetFn
);
610 if( layerDescGetFn
) {
611 *pkKeyboardLayout
= layerDescGetFn();
615 if( !*pkKeyboardLayout
) {
616 DPRINT1("Failed to load the keyboard layout.\n");
622 PKBDTABLES
W32kGetDefaultKeyLayout() {
623 PKBDTABLES pkKeyboardLayout
= 0;
624 InitKbdLayout( (PVOID
) &pkKeyboardLayout
);
625 return pkKeyboardLayout
;
629 IntTranslateKbdMessage(LPMSG lpMsg
,
632 static INT dead_char
= 0;
636 PKBDTABLES keyLayout
;
641 keyLayout
= PsGetWin32Thread()->KeyboardLayout
;
645 if (lpMsg
->message
!= WM_KEYDOWN
&& lpMsg
->message
!= WM_SYSKEYDOWN
)
648 ScanCode
= (lpMsg
->lParam
>> 16) & 0xff;
652 /* All messages have to contain the cursor point. */
653 IntGetCursorLocation(PsGetWin32Thread()->Desktop
->WindowStation
,
656 UState
= ToUnicodeInner(lpMsg
->wParam
, HIWORD(lpMsg
->lParam
) & 0xff,
657 QueueKeyStateTable
, wp
, 2, 0,
662 NewMsg
.message
= (lpMsg
->message
== WM_KEYDOWN
) ? WM_CHAR
: WM_SYSCHAR
;
667 DPRINT("PREVIOUS DEAD CHAR: %c\n", dead_char
);
669 for( i
= 0; keyLayout
->pDeadKey
[i
].dwBoth
; i
++ )
671 first
= keyLayout
->pDeadKey
[i
].dwBoth
>> 16;
672 second
= keyLayout
->pDeadKey
[i
].dwBoth
;
673 if (first
== dead_char
&& second
== wp
[0])
675 wp
[0] = keyLayout
->pDeadKey
[i
].wchComposed
;
681 DPRINT("FINAL CHAR: %c\n", wp
[0]);
686 NewMsg
.hwnd
= lpMsg
->hwnd
;
687 NewMsg
.wParam
= dead_char
;
688 NewMsg
.lParam
= lpMsg
->lParam
;
690 MsqPostMessage(PsGetWin32Thread()->MessageQueue
, &NewMsg
, FALSE
, QS_KEY
);
693 NewMsg
.hwnd
= lpMsg
->hwnd
;
694 NewMsg
.wParam
= wp
[0];
695 NewMsg
.lParam
= lpMsg
->lParam
;
696 DPRINT( "CHAR='%c' %04x %08x\n", wp
[0], wp
[0], lpMsg
->lParam
);
697 MsqPostMessage(PsGetWin32Thread()->MessageQueue
, &NewMsg
, FALSE
, QS_KEY
);
700 else if (UState
== -1)
703 (lpMsg
->message
== WM_KEYDOWN
) ? WM_DEADCHAR
: WM_SYSDEADCHAR
;
704 NewMsg
.hwnd
= lpMsg
->hwnd
;
705 NewMsg
.wParam
= wp
[0];
706 NewMsg
.lParam
= lpMsg
->lParam
;
708 MsqPostMessage(PsGetWin32Thread()->MessageQueue
, &NewMsg
, FALSE
, QS_KEY
);
718 NtUserGetKeyboardState(
725 if(!NT_SUCCESS(MmCopyToCaller(lpKeyState
, QueueKeyStateTable
, 256)))
734 NtUserSetKeyboardState(
741 if(! NT_SUCCESS(MmCopyFromCaller(QueueKeyStateTable
, lpKeyState
, 256)))
749 static UINT
VkToScan( UINT Code
, BOOL ExtCode
, PKBDTABLES pkKT
) {
752 for( i
= 0; i
< pkKT
->bMaxVSCtoVK
; i
++ ) {
753 if( pkKT
->pusVSCtoVK
[i
] == Code
) { return i
; }
759 UINT
ScanToVk( UINT Code
, BOOL ExtKey
, PKBDTABLES pkKT
) {
761 DPRINT("ScanToVk: No layout\n");
768 for( i
= 0; pkKT
->pVSCtoVK_E0
[i
].Vsc
; i
++ ) {
769 if( pkKT
->pVSCtoVK_E0
[i
].Vsc
== Code
)
770 return pkKT
->pVSCtoVK_E0
[i
].Vk
& 0xff;
772 for( i
= 0; pkKT
->pVSCtoVK_E1
[i
].Vsc
; i
++ ) {
773 if( pkKT
->pVSCtoVK_E1
[i
].Vsc
== Code
)
774 return pkKT
->pVSCtoVK_E1
[i
].Vk
& 0xff;
779 if( Code
>= pkKT
->bMaxVSCtoVK
) { return 0; }
780 return pkKT
->pusVSCtoVK
[Code
] & 0xff;
785 * Map a virtual key code, or virtual scan code, to a scan code, key code,
786 * or unshifted unicode character.
790 * 0 -- Code is a virtual key code that is converted into a virtual scan code
791 * that does not distinguish between left and right shift keys.
792 * 1 -- Code is a virtual scan code that is converted into a virtual key code
793 * that does not distinguish between left and right shift keys.
794 * 2 -- Code is a virtual key code that is converted into an unshifted unicode
796 * 3 -- Code is a virtual scan code that is converted into a virtual key code
797 * that distinguishes left and right shift keys.
798 * KeyLayout: Keyboard layout handle (currently, unused)
803 static UINT
IntMapVirtualKeyEx( UINT Code
, UINT Type
, PKBDTABLES keyLayout
) {
808 if( Code
== VK_RSHIFT
) Code
= VK_LSHIFT
;
809 if( Code
== VK_RMENU
) Code
= VK_LMENU
;
810 if( Code
== VK_RCONTROL
) Code
= VK_LCONTROL
;
811 ret
= VkToScan( Code
, FALSE
, keyLayout
);
816 DontDistinguishShifts
817 (IntMapVirtualKeyEx( Code
, 3, keyLayout
) );
823 ret
= VkToScan( Code
, FALSE
, keyLayout
);
824 ToUnicodeInner( Code
, ret
, 0, wp
, 2, 0, keyLayout
);
830 ret
= ScanToVk( Code
, FALSE
, keyLayout
);
839 NtUserMapVirtualKeyEx( UINT Code
, UINT Type
, DWORD keyboardId
, HKL dwhkl
) {
840 PKBDTABLES keyLayout
= PsGetWin32Thread() ?
841 PsGetWin32Thread()->KeyboardLayout
: 0;
843 if( !keyLayout
) return 0;
845 return IntMapVirtualKeyEx( Code
, Type
, keyLayout
);
859 BYTE KeyStateBuf
[0x100];
860 PWCHAR OutPwszBuff
= 0;
864 if( !NT_SUCCESS(MmCopyFromCaller(KeyStateBuf
,
866 sizeof(KeyStateBuf
))) ) {
867 DPRINT1( "Couldn't copy key state from caller.\n" );
870 OutPwszBuff
= ExAllocatePoolWithTag(NonPagedPool
,sizeof(WCHAR
) * cchBuff
, TAG_STRING
);
872 DPRINT1( "ExAllocatePool(%d) failed\n", sizeof(WCHAR
) * cchBuff
);
875 RtlZeroMemory( OutPwszBuff
, sizeof( WCHAR
) * cchBuff
);
877 ret
= ToUnicodeEx( wVirtKey
,
885 MmCopyToCaller(pwszBuff
,OutPwszBuff
,sizeof(WCHAR
)*cchBuff
);
886 ExFreePool(OutPwszBuff
);
891 static int W32kSimpleToupper( int ch
) {
892 if( ch
>= 'a' && ch
<= 'z' ) ch
= ch
- 'a' + 'A';
898 NtUserGetKeyNameText( LONG lParam
, LPWSTR lpString
, int nSize
) {
903 UINT ScanCode
= (lParam
>> 16) & 0xff;
904 BOOL ExtKey
= lParam
& (1<<24) ? TRUE
: FALSE
;
905 PKBDTABLES keyLayout
=
907 PsGetWin32Thread()->KeyboardLayout
: 0;
909 if( !keyLayout
|| nSize
< 1 ) return 0;
911 if( lParam
& (1<<25) ) {
912 CareVk
= VkCode
= ScanToVk( ScanCode
, ExtKey
, keyLayout
);
913 if( VkCode
== VK_LSHIFT
|| VkCode
== VK_RSHIFT
)
915 if( VkCode
== VK_LCONTROL
|| VkCode
== VK_RCONTROL
)
916 VkCode
= VK_LCONTROL
;
917 if( VkCode
== VK_LMENU
|| VkCode
== VK_RMENU
)
920 VkCode
= ScanToVk( ScanCode
, ExtKey
, keyLayout
);
923 VSC_LPWSTR
*KeyNames
= 0;
925 if( CareVk
!= VkCode
)
926 ScanCode
= VkToScan( VkCode
, ExtKey
, keyLayout
);
929 KeyNames
= keyLayout
->pKeyNamesExt
;
931 KeyNames
= keyLayout
->pKeyNames
;
933 for( i
= 0; KeyNames
[i
].pwsz
; i
++ ) {
934 if( KeyNames
[i
].vsc
== ScanCode
) {
935 UINT StrLen
= wcslen(KeyNames
[i
].pwsz
);
936 UINT StrMax
= StrLen
> (nSize
- 1) ? (nSize
- 1) : StrLen
;
938 if( NT_SUCCESS( MmCopyToCaller( lpString
,
940 StrMax
* sizeof(WCHAR
) ) ) &&
941 NT_SUCCESS( MmCopyToCaller( lpString
+ StrMax
,
943 sizeof( WCHAR
) ) ) ) {
953 UCName
[0] = W32kSimpleToupper(IntMapVirtualKeyEx( VkCode
, 2, keyLayout
));
957 if( !NT_SUCCESS(MmCopyToCaller( lpString
, UCName
, 2 * sizeof(WCHAR
) )) )
965 * Filter this message according to the current key layout, setting wParam
970 W32kKeyProcessMessage(LPMSG Msg
,
971 PKBDTABLES KeyboardLayout
,
974 DWORD ScanCode
= 0, ModifierBits
= 0;
976 DWORD BaseMapping
= 0;
978 static WORD NumpadConversion
[][2] =
979 { { VK_DELETE
, VK_DECIMAL
},
980 { VK_INSERT
, VK_NUMPAD0
},
981 { VK_END
, VK_NUMPAD1
},
982 { VK_DOWN
, VK_NUMPAD2
},
983 { VK_NEXT
, VK_NUMPAD3
},
984 { VK_LEFT
, VK_NUMPAD4
},
985 { VK_CLEAR
, VK_NUMPAD5
},
986 { VK_RIGHT
, VK_NUMPAD6
},
987 { VK_HOME
, VK_NUMPAD7
},
988 { VK_UP
, VK_NUMPAD8
},
989 { VK_PRIOR
, VK_NUMPAD9
},
993 if( !KeyboardLayout
|| !Msg
||
994 (Msg
->message
!= WM_KEYDOWN
&& Msg
->message
!= WM_SYSKEYDOWN
&&
995 Msg
->message
!= WM_KEYUP
&& Msg
->message
!= WM_SYSKEYUP
) )
1002 /* arty -- handle numpad -- On real windows, the actual key produced
1003 * by the messaging layer is different based on the state of numlock. */
1004 ModifierBits
= ModBits(KeyboardLayout
,QueueKeyStateTable
);
1006 /* Get the raw scan code, so we can look up whether the key is a numpad
1009 * Shift and the LP_EXT_BIT cancel. */
1010 ScanCode
= (Msg
->lParam
>> 16) & 0xff;
1011 BaseMapping
= Msg
->wParam
=
1012 IntMapVirtualKeyEx( ScanCode
, 1, KeyboardLayout
);
1015 if( ScanCode
>= KeyboardLayout
->bMaxVSCtoVK
)
1018 RawVk
= KeyboardLayout
->pusVSCtoVK
[ScanCode
];
1022 if( Prefix
== 0xE0 )
1024 /* ignore shift codes */
1025 if( ScanCode
== 0x2A || ScanCode
== 0x36 )
1027 IntUnLockQueueState
;
1030 VscVkTable
= KeyboardLayout
->pVSCtoVK_E0
;
1032 else if( Prefix
== 0xE1 )
1034 VscVkTable
= KeyboardLayout
->pVSCtoVK_E1
;
1038 while (VscVkTable
->Vsc
)
1040 if( VscVkTable
->Vsc
== ScanCode
)
1042 RawVk
= VscVkTable
->Vk
;
1048 if ((ModifierBits
& NUMLOCK_BIT
) &&
1049 !(ModifierBits
& GetShiftBit(KeyboardLayout
, VK_SHIFT
)) &&
1051 !(Msg
->lParam
& LP_EXT_BIT
))
1053 /* The key in question is a numpad key. Search for a translation. */
1054 for (i
= 0; NumpadConversion
[i
][0]; i
++)
1056 if ((BaseMapping
& 0xff) == NumpadConversion
[i
][0]) /* RawVk? */
1058 Msg
->wParam
= NumpadConversion
[i
][1];
1064 DPRINT("Key: [%04x -> %04x]\n", BaseMapping
, Msg
->wParam
);
1066 /* Now that we have the VK, we can set the keymap appropriately
1067 * This is a better place for this code, as it's guaranteed to be
1068 * run, unlike translate message. */
1069 if (Msg
->message
== WM_KEYDOWN
|| Msg
->message
== WM_SYSKEYDOWN
)
1071 SetKeyState( ScanCode
, Msg
->wParam
, Msg
->lParam
& LP_EXT_BIT
,
1072 TRUE
); /* Strike key */
1074 else if (Msg
->message
== WM_KEYUP
|| Msg
->message
== WM_SYSKEYUP
)
1076 SetKeyState( ScanCode
, Msg
->wParam
, Msg
->lParam
& LP_EXT_BIT
,
1077 FALSE
); /* Release key */
1080 /* We need to unset SYSKEYDOWN if the ALT key is an ALT+Gr */
1081 if( QueueKeyStateTable
[VK_RMENU
] & KS_DOWN_BIT
) {
1082 if( Msg
->message
== WM_SYSKEYDOWN
) Msg
->message
= WM_KEYDOWN
;
1083 else Msg
->message
= WM_KEYUP
;
1086 IntUnLockQueueState
;
1091 NtUserGetKeyboardLayoutList(
1102 NtUserGetKeyboardLayoutName(
1112 NtUserGetKeyboardLayout(
1117 PW32THREAD W32Thread
;
1121 W32Thread
= PsGetWin32Thread();
1124 Status
= PsLookupThreadByThreadId((HANDLE
)dwThreadId
, &Thread
);
1125 if(!NT_SUCCESS(Status
))
1127 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
1130 W32Thread
= Thread
->Tcb
.Win32Thread
;
1132 layout
= W32Thread
->KeyboardLayout
;
1133 if(!layout
) return 0;
1140 NtUserGetKeyboardType(
1145 case 0: /* Keyboard type */
1146 return 4; /* AT-101 */
1147 case 1: /* Keyboard Subtype */
1148 return 0; /* There are no defined subtypes */
1149 case 2: /* Number of F-keys */
1150 return 12; /* We're doing an 101 for now, so return 12 F-keys */
1152 DPRINT1("Unknown type!\n");
1153 return 0; /* The book says 0 here, so 0 */
1159 Based on TryToTranslateChar, instead of processing VirtualKey match,
1160 look for wChar match.
1166 DWORD KeyboardLayout
,
1169 PKBDTABLES KeyLayout
;
1170 PVK_TO_WCHAR_TABLE vtwTbl
;
1171 PVK_TO_WCHARS10 vkPtr
;
1172 size_t size_this_entry
;
1174 DWORD CapsMod
= 0, CapsState
= 0;
1176 if(!KeyboardLayout
) return -1;
1177 KeyLayout
= (PKBDTABLES
) KeyboardLayout
;
1179 for (nMod
= 0; KeyLayout
->pVkToWcharTable
[nMod
].nModifications
; nMod
++)
1181 vtwTbl
= &KeyLayout
->pVkToWcharTable
[nMod
];
1182 size_this_entry
= vtwTbl
->cbSize
;
1183 vkPtr
= (PVK_TO_WCHARS10
)((BYTE
*)vtwTbl
->pVkToWchars
);
1185 while(vkPtr
->VirtualKey
)
1191 Should have only 8 valid possibilities. Including zero.
1193 for(CapsState
= 0; CapsState
< vtwTbl
->nModifications
; CapsState
++)
1195 if(vkPtr
->wch
[CapsState
] == wChar
)
1197 CapsMod
= KeyLayout
->pCharModifiers
->ModNumber
[CapsState
];
1198 DPRINT("nMod %d wC %04x: CapsMod %08x CapsState %08x MaxModBits %08x\n",
1199 nMod
, wChar
, CapsMod
, CapsState
, KeyLayout
->pCharModifiers
->wMaxModBits
);
1200 return ((CapsMod
<< 8)|(vkPtr
->VirtualKey
& 0xff));
1203 vkPtr
= (PVK_TO_WCHARS10
)(((BYTE
*)vkPtr
) + size_this_entry
);