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
)
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
)
79 if( ret
== VK_LSHIFT
|| ret
== VK_RSHIFT
)
81 if( ret
== VK_LCONTROL
|| ret
== VK_RCONTROL
)
83 if( ret
== VK_LMENU
|| ret
== VK_RMENU
)
88 static VOID STDCALL
SetKeyState(DWORD key
, DWORD vk
, DWORD ext
, BOOL down
)
92 /* Special handling for toggles like numpad and caps lock */
93 if (vk
== VK_CAPITAL
|| vk
== VK_NUMLOCK
)
96 QueueKeyStateTable
[vk
] ^= KS_LOCK_BIT
;
99 if (ext
&& vk
== VK_LSHIFT
)
101 if (ext
&& vk
== VK_LCONTROL
)
103 if (ext
&& vk
== VK_LMENU
)
107 QueueKeyStateTable
[vk
] |= KS_DOWN_BIT
;
109 QueueKeyStateTable
[vk
] &= ~KS_DOWN_MASK
;
111 if (vk
== VK_LSHIFT
|| vk
== VK_RSHIFT
)
113 if ((QueueKeyStateTable
[VK_LSHIFT
] & KS_DOWN_BIT
) ||
114 (QueueKeyStateTable
[VK_RSHIFT
] & KS_DOWN_BIT
))
116 QueueKeyStateTable
[VK_SHIFT
] |= KS_DOWN_BIT
;
120 QueueKeyStateTable
[VK_SHIFT
] &= ~KS_DOWN_MASK
;
124 if (vk
== VK_LCONTROL
|| vk
== VK_RCONTROL
)
126 if ((QueueKeyStateTable
[VK_LCONTROL
] & KS_DOWN_BIT
) ||
127 (QueueKeyStateTable
[VK_RCONTROL
] & KS_DOWN_BIT
))
129 QueueKeyStateTable
[VK_CONTROL
] |= KS_DOWN_BIT
;
133 QueueKeyStateTable
[VK_CONTROL
] &= ~KS_DOWN_MASK
;
137 if (vk
== VK_LMENU
|| vk
== VK_RMENU
)
139 if ((QueueKeyStateTable
[VK_LMENU
] & KS_DOWN_BIT
) ||
140 (QueueKeyStateTable
[VK_RMENU
] & KS_DOWN_BIT
))
142 QueueKeyStateTable
[VK_MENU
] |= KS_DOWN_BIT
;
146 QueueKeyStateTable
[VK_MENU
] &= ~KS_DOWN_MASK
;
151 VOID
DumpKeyState( PBYTE KeyState
)
155 DbgPrint( "KeyState { " );
156 for( i
= 0; i
< 0x100; i
++ )
159 DbgPrint( "%02x(%02x) ", i
, KeyState
[i
] );
164 static BYTE
KeysSet( PKBDTABLES pkKT
, PBYTE KeyState
,
165 int FakeModLeft
, int FakeModRight
)
167 if( !KeyState
|| !pkKT
)
170 /* Search special codes first */
171 if( FakeModLeft
&& KeyState
[FakeModLeft
] )
172 return KeyState
[FakeModLeft
];
173 else if( FakeModRight
&& KeyState
[FakeModRight
] )
174 return KeyState
[FakeModRight
];
179 /* Search the keyboard layout modifiers table for the shift bit. I don't
180 * want to count on the shift bit not moving, because it can be specified
183 static DWORD FASTCALL
GetShiftBit( PKBDTABLES pkKT
, DWORD Vk
)
187 for( i
= 0; pkKT
->pCharModifiers
->pVkToBit
[i
].Vk
; i
++ )
188 if( pkKT
->pCharModifiers
->pVkToBit
[i
].Vk
== Vk
)
189 return pkKT
->pCharModifiers
->pVkToBit
[i
].ModBits
;
194 static DWORD
ModBits( PKBDTABLES pkKT
, PBYTE KeyState
)
201 /* DumpKeyState( KeyState ); */
203 if (KeysSet( pkKT
, KeyState
, VK_LSHIFT
, VK_RSHIFT
) &
205 ModBits
|= GetShiftBit( pkKT
, VK_SHIFT
);
207 if (KeysSet( pkKT
, KeyState
, VK_LCONTROL
, VK_RCONTROL
) &
209 ModBits
|= GetShiftBit( pkKT
, VK_CONTROL
);
211 if (KeysSet( pkKT
, KeyState
, VK_LMENU
, VK_RMENU
) &
213 ModBits
|= GetShiftBit( pkKT
, VK_MENU
);
216 if (KeysSet( pkKT
, KeyState
, VK_RMENU
, 0 ) &
218 ModBits
|= GetShiftBit( pkKT
, VK_CONTROL
);
220 /* Deal with VK_CAPITAL */
221 if (KeysSet( pkKT
, KeyState
, VK_CAPITAL
, 0 ) & KS_LOCK_BIT
)
223 ModBits
|= CAPITAL_BIT
;
226 /* Deal with VK_NUMLOCK */
227 if (KeysSet( pkKT
, KeyState
, VK_NUMLOCK
, 0 ) & KS_LOCK_BIT
)
229 ModBits
|= NUMLOCK_BIT
;
232 DPRINT( "Current Mod Bits: %x\n", ModBits
);
237 static BOOL
TryToTranslateChar(WORD wVirtKey
,
241 PWCHAR pwcTranslatedChar
,
242 PKBDTABLES keyLayout
)
244 PVK_TO_WCHAR_TABLE vtwTbl
;
245 PVK_TO_WCHARS10 vkPtr
;
246 size_t size_this_entry
;
248 DWORD CapsMod
= 0, CapsState
= 0;
250 CapsState
= ModBits
& ~MOD_BITS_MASK
;
251 ModBits
= ModBits
& MOD_BITS_MASK
;
253 DPRINT ( "TryToTranslate: %04x %x\n", wVirtKey
, ModBits
);
255 if (ModBits
> keyLayout
->pCharModifiers
->wMaxModBits
)
259 for (nMod
= 0; keyLayout
->pVkToWcharTable
[nMod
].nModifications
; nMod
++)
261 vtwTbl
= &keyLayout
->pVkToWcharTable
[nMod
];
262 size_this_entry
= vtwTbl
->cbSize
;
263 vkPtr
= (PVK_TO_WCHARS10
)((BYTE
*)vtwTbl
->pVkToWchars
);
264 while(vkPtr
->VirtualKey
)
266 if( wVirtKey
== (vkPtr
->VirtualKey
& 0xff) )
268 CapsMod
= keyLayout
->pCharModifiers
->ModNumber
270 ((CapsState
& CAPITAL_BIT
) ? vkPtr
->Attributes
: 0)];
272 if( CapsMod
> keyLayout
->pVkToWcharTable
[nMod
].nModifications
)
276 keyLayout
->pVkToWcharTable
[nMod
].nModifications
)
279 CapsMod
&= MaxBit
- 1; /* Guarantee that CapsMod lies
283 *pbDead
= vkPtr
->wch
[CapsMod
] == WCH_DEAD
;
284 *pbLigature
= vkPtr
->wch
[CapsMod
] == WCH_LGTR
;
285 *pwcTranslatedChar
= vkPtr
->wch
[CapsMod
];
287 DPRINT("%d %04x: CapsMod %08x CapsState %08x Char %04x\n",
289 CapsMod
, CapsState
, *pwcTranslatedChar
);
293 vkPtr
= (PVK_TO_WCHARS10
)(((BYTE
*)vkPtr
) + size_this_entry
);
294 if( vkPtr
->VirtualKey
!= 0xff )
296 DPRINT( "Found dead key with no trailer in the table.\n" );
297 DPRINT( "VK: %04x, ADDR: %08x\n", wVirtKey
, (int)vkPtr
);
300 *pwcTranslatedChar
= vkPtr
->wch
[CapsMod
];
304 vkPtr
= (PVK_TO_WCHARS10
)(((BYTE
*)vkPtr
) + size_this_entry
);
312 ToUnicodeInner(UINT wVirtKey
,
320 WCHAR wcTranslatedChar
;
327 if( TryToTranslateChar( wVirtKey
,
328 ModBits( pkKT
, lpKeyState
),
336 DPRINT("Not handling ligature (yet)\n" );
341 pwszBuff
[0] = wcTranslatedChar
;
343 return bDead
? -1 : 1;
350 DWORD FASTCALL
UserGetKeyState(DWORD key
)
357 ret
= ((DWORD
)(QueueKeyStateTable
[key
] & KS_DOWN_BIT
) << 8 ) |
358 (QueueKeyStateTable
[key
] & KS_LOCK_BIT
);
370 DECLARE_RETURN(DWORD
);
372 DPRINT("Enter NtUserGetKeyState\n");
373 UserEnterExclusive();
375 RETURN(UserGetKeyState(key
));
378 DPRINT("Leave NtUserGetKeyState, ret=%i\n",_ret_
);
385 DWORD FASTCALL
UserGetAsyncKeyState(DWORD key
)
392 ret
= ((DWORD
)(QueueKeyStateTable
[key
] & KS_DOWN_BIT
) << 8 ) |
393 (QueueKeyStateTable
[key
] & KS_LOCK_BIT
);
403 NtUserGetAsyncKeyState(
406 DECLARE_RETURN(DWORD
);
408 DPRINT("Enter NtUserGetAsyncKeyState\n");
409 UserEnterExclusive();
411 RETURN(UserGetAsyncKeyState(key
));
414 DPRINT("Leave NtUserGetAsyncKeyState, ret=%i\n",_ret_
);
421 int STDCALL
ToUnicodeEx( UINT wVirtKey
,
429 int ToUnicodeResult
= 0;
431 if (0 == (lpKeyState
[wVirtKey
] & KS_DOWN_BIT
))
438 ToUnicodeResult
= ToUnicodeInner( wVirtKey
,
445 PsGetWin32Thread()->KeyboardLayout
: 0 );
449 return ToUnicodeResult
;
452 int STDCALL
ToUnicode( UINT wVirtKey
,
459 return ToUnicodeEx( wVirtKey
,
469 * Utility to copy and append two unicode strings.
471 * IN OUT PUNICODE_STRING ResultFirst -> First string and result
472 * IN PUNICODE_STRING Second -> Second string to append
473 * IN BOOL Deallocate -> TRUE: Deallocate First string before
479 NTSTATUS NTAPI
AppendUnicodeString(PUNICODE_STRING ResultFirst
,
480 PUNICODE_STRING Second
,
485 ExAllocatePoolWithTag(PagedPool
,
486 (ResultFirst
->Length
+ Second
->Length
+ sizeof(WCHAR
)),
490 return STATUS_NO_MEMORY
;
492 memcpy( new_string
, ResultFirst
->Buffer
,
493 ResultFirst
->Length
);
494 memcpy( new_string
+ ResultFirst
->Length
/ sizeof(WCHAR
),
498 RtlFreeUnicodeString(ResultFirst
);
499 ResultFirst
->Length
+= Second
->Length
;
500 ResultFirst
->MaximumLength
= ResultFirst
->Length
;
501 new_string
[ResultFirst
->Length
/ sizeof(WCHAR
)] = 0;
502 Status
= RtlCreateUnicodeString(ResultFirst
,new_string
) ?
503 STATUS_SUCCESS
: STATUS_NO_MEMORY
;
504 ExFreePool(new_string
);
509 * Utility function to read a value from the registry more easily.
511 * IN PUNICODE_STRING KeyName -> Name of key to open
512 * IN PUNICODE_STRING ValueName -> Name of value to open
513 * OUT PUNICODE_STRING ReturnedValue -> String contained in registry
518 static NTSTATUS NTAPI
ReadRegistryValue( PUNICODE_STRING KeyName
,
519 PUNICODE_STRING ValueName
,
520 PUNICODE_STRING ReturnedValue
)
524 OBJECT_ATTRIBUTES KeyAttributes
;
525 PKEY_VALUE_PARTIAL_INFORMATION KeyValuePartialInfo
;
530 InitializeObjectAttributes(&KeyAttributes
, KeyName
, OBJ_CASE_INSENSITIVE
,
532 Status
= ZwOpenKey(&KeyHandle
, KEY_ALL_ACCESS
, &KeyAttributes
);
533 if( !NT_SUCCESS(Status
) )
538 Status
= ZwQueryValueKey(KeyHandle
, ValueName
, KeyValuePartialInformation
,
543 if( Status
!= STATUS_BUFFER_TOO_SMALL
)
549 ResLength
+= sizeof( *KeyValuePartialInfo
);
550 KeyValuePartialInfo
=
551 ExAllocatePoolWithTag(PagedPool
, ResLength
, TAG_STRING
);
554 if( !KeyValuePartialInfo
)
557 return STATUS_NO_MEMORY
;
560 Status
= ZwQueryValueKey(KeyHandle
, ValueName
, KeyValuePartialInformation
,
561 (PVOID
)KeyValuePartialInfo
,
565 if( !NT_SUCCESS(Status
) )
568 ExFreePool(KeyValuePartialInfo
);
572 Temp
.Length
= Temp
.MaximumLength
= KeyValuePartialInfo
->DataLength
;
573 Temp
.Buffer
= (PWCHAR
)KeyValuePartialInfo
->Data
;
575 /* At this point, KeyValuePartialInfo->Data contains the key data */
576 RtlInitUnicodeString(ReturnedValue
,L
"");
577 AppendUnicodeString(ReturnedValue
,&Temp
,FALSE
);
579 ExFreePool(KeyValuePartialInfo
);
585 typedef PVOID (*KbdLayerDescriptor
)(VOID
);
586 NTSTATUS STDCALL
LdrGetProcedureAddress(PVOID module
,
587 PANSI_STRING import_name
,
591 void InitKbdLayout( PVOID
*pkKeyboardLayout
)
593 WCHAR LocaleBuffer
[16];
594 UNICODE_STRING LayoutKeyName
;
595 UNICODE_STRING LayoutValueName
;
596 UNICODE_STRING DefaultLocale
;
597 UNICODE_STRING LayoutFile
;
598 UNICODE_STRING FullLayoutPath
;
600 PWCHAR KeyboardLayoutWSTR
;
601 HMODULE kbModule
= 0;
603 ANSI_STRING kbdProcedureName
;
604 KbdLayerDescriptor layerDescGetFn
;
606 #define XX_STATUS(x) if (!NT_SUCCESS(Status = (x))) continue;
610 Status
= ZwQueryDefaultLocale(FALSE
, &LocaleId
);
611 if (!NT_SUCCESS(Status
))
613 DPRINT1("Could not get default locale (%08lx).\n", Status
);
617 DPRINT("DefaultLocale = %lx\n", LocaleId
);
618 swprintf(LocaleBuffer
, L
"%08lx", LocaleId
);
619 DPRINT("DefaultLocale = %S\n", LocaleBuffer
);
620 RtlInitUnicodeString(&DefaultLocale
, LocaleBuffer
);
622 RtlInitUnicodeString(&LayoutKeyName
,
623 L
"\\REGISTRY\\Machine\\SYSTEM\\CurrentControlSet"
624 L
"\\Control\\KeyboardLayouts\\");
626 AppendUnicodeString(&LayoutKeyName
,&DefaultLocale
,FALSE
);
628 RtlInitUnicodeString(&LayoutValueName
,L
"Layout File");
630 Status
= ReadRegistryValue(&LayoutKeyName
,&LayoutValueName
,&LayoutFile
);
631 RtlInitUnicodeString(&FullLayoutPath
,SYSTEMROOT_DIR
);
633 if( !NT_SUCCESS(Status
) )
635 DPRINT1("Got default locale but not layout file. (%08lx)\n",
640 DPRINT("Read registry and got %wZ\n", &LayoutFile
);
642 RtlFreeUnicodeString(&LayoutKeyName
);
644 AppendUnicodeString(&FullLayoutPath
,&LayoutFile
,FALSE
);
646 DPRINT("Loading Keyboard DLL %wZ\n", &FullLayoutPath
);
648 RtlFreeUnicodeString(&LayoutFile
);
651 ExAllocatePoolWithTag(PagedPool
,
652 FullLayoutPath
.Length
+ sizeof(WCHAR
),
655 if( !KeyboardLayoutWSTR
)
657 DPRINT1("Couldn't allocate a string for the keyboard layout name.\n");
658 RtlFreeUnicodeString(&FullLayoutPath
);
661 memcpy(KeyboardLayoutWSTR
,FullLayoutPath
.Buffer
,
662 FullLayoutPath
.Length
+ sizeof(WCHAR
));
663 KeyboardLayoutWSTR
[FullLayoutPath
.Length
/ sizeof(WCHAR
)] = 0;
665 kbModule
= EngLoadImage(KeyboardLayoutWSTR
);
666 DPRINT( "Load Keyboard Layout: %S\n", KeyboardLayoutWSTR
);
669 DPRINT1( "Load Keyboard Layout: No %wZ\n", &FullLayoutPath
);
671 RtlFreeUnicodeString(&FullLayoutPath
);
677 DPRINT1("Trying to load US Keyboard Layout\n");
678 kbModule
= EngLoadImage(L
"\\SystemRoot\\system32\\kbdus.dll");
682 DPRINT1("Failed to load any Keyboard Layout\n");
687 RtlInitAnsiString( &kbdProcedureName
, "KbdLayerDescriptor" );
689 LdrGetProcedureAddress((PVOID
)kbModule
,
692 (PVOID
*)&layerDescGetFn
);
696 *pkKeyboardLayout
= layerDescGetFn();
701 if( !*pkKeyboardLayout
)
703 DPRINT1("Failed to load the keyboard layout.\n");
709 PKBDTABLES
W32kGetDefaultKeyLayout()
711 PKBDTABLES pkKeyboardLayout
= 0;
712 InitKbdLayout( (PVOID
) &pkKeyboardLayout
);
713 return pkKeyboardLayout
;
717 IntTranslateKbdMessage(LPMSG lpMsg
,
720 static INT dead_char
= 0;
724 PKBDTABLES keyLayout
;
729 keyLayout
= PsGetWin32Thread()->KeyboardLayout
;
733 if (lpMsg
->message
!= WM_KEYDOWN
&& lpMsg
->message
!= WM_SYSKEYDOWN
)
736 ScanCode
= (lpMsg
->lParam
>> 16) & 0xff;
740 /* All messages have to contain the cursor point. */
741 IntGetCursorLocation(PsGetWin32Thread()->Desktop
->WindowStation
,
744 UState
= ToUnicodeInner(lpMsg
->wParam
, HIWORD(lpMsg
->lParam
) & 0xff,
745 QueueKeyStateTable
, wp
, 2, 0,
750 NewMsg
.message
= (lpMsg
->message
== WM_KEYDOWN
) ? WM_CHAR
: WM_SYSCHAR
;
755 DPRINT("PREVIOUS DEAD CHAR: %c\n", dead_char
);
757 for( i
= 0; keyLayout
->pDeadKey
[i
].dwBoth
; i
++ )
759 first
= keyLayout
->pDeadKey
[i
].dwBoth
>> 16;
760 second
= keyLayout
->pDeadKey
[i
].dwBoth
;
761 if (first
== dead_char
&& second
== wp
[0])
763 wp
[0] = keyLayout
->pDeadKey
[i
].wchComposed
;
769 DPRINT("FINAL CHAR: %c\n", wp
[0]);
774 NewMsg
.hwnd
= lpMsg
->hwnd
;
775 NewMsg
.wParam
= dead_char
;
776 NewMsg
.lParam
= lpMsg
->lParam
;
778 MsqPostMessage(PsGetWin32Thread()->MessageQueue
, &NewMsg
, FALSE
, QS_KEY
);
781 NewMsg
.hwnd
= lpMsg
->hwnd
;
782 NewMsg
.wParam
= wp
[0];
783 NewMsg
.lParam
= lpMsg
->lParam
;
784 DPRINT( "CHAR='%c' %04x %08x\n", wp
[0], wp
[0], lpMsg
->lParam
);
785 MsqPostMessage(PsGetWin32Thread()->MessageQueue
, &NewMsg
, FALSE
, QS_KEY
);
788 else if (UState
== -1)
791 (lpMsg
->message
== WM_KEYDOWN
) ? WM_DEADCHAR
: WM_SYSDEADCHAR
;
792 NewMsg
.hwnd
= lpMsg
->hwnd
;
793 NewMsg
.wParam
= wp
[0];
794 NewMsg
.lParam
= lpMsg
->lParam
;
796 MsqPostMessage(PsGetWin32Thread()->MessageQueue
, &NewMsg
, FALSE
, QS_KEY
);
806 NtUserGetKeyboardState(
814 if(!NT_SUCCESS(MmCopyToCaller(lpKeyState
, QueueKeyStateTable
, 256)))
823 NtUserSetKeyboardState(
831 if(! NT_SUCCESS(MmCopyFromCaller(QueueKeyStateTable
, lpKeyState
, 256)))
839 static UINT
VkToScan( UINT Code
, BOOL ExtCode
, PKBDTABLES pkKT
)
843 for( i
= 0; i
< pkKT
->bMaxVSCtoVK
; i
++ )
845 if( pkKT
->pusVSCtoVK
[i
] == Code
)
854 UINT
ScanToVk( UINT Code
, BOOL ExtKey
, PKBDTABLES pkKT
)
858 DPRINT("ScanToVk: No layout\n");
866 for( i
= 0; pkKT
->pVSCtoVK_E0
[i
].Vsc
; i
++ )
868 if( pkKT
->pVSCtoVK_E0
[i
].Vsc
== Code
)
869 return pkKT
->pVSCtoVK_E0
[i
].Vk
& 0xff;
871 for( i
= 0; pkKT
->pVSCtoVK_E1
[i
].Vsc
; i
++ )
873 if( pkKT
->pVSCtoVK_E1
[i
].Vsc
== Code
)
874 return pkKT
->pVSCtoVK_E1
[i
].Vk
& 0xff;
881 if( Code
>= pkKT
->bMaxVSCtoVK
)
885 return pkKT
->pusVSCtoVK
[Code
] & 0xff;
890 * Map a virtual key code, or virtual scan code, to a scan code, key code,
891 * or unshifted unicode character.
895 * 0 -- Code is a virtual key code that is converted into a virtual scan code
896 * that does not distinguish between left and right shift keys.
897 * 1 -- Code is a virtual scan code that is converted into a virtual key code
898 * that does not distinguish between left and right shift keys.
899 * 2 -- Code is a virtual key code that is converted into an unshifted unicode
901 * 3 -- Code is a virtual scan code that is converted into a virtual key code
902 * that distinguishes left and right shift keys.
903 * KeyLayout: Keyboard layout handle (currently, unused)
908 static UINT
IntMapVirtualKeyEx( UINT Code
, UINT Type
, PKBDTABLES keyLayout
)
915 if( Code
== VK_RSHIFT
)
917 if( Code
== VK_RMENU
)
919 if( Code
== VK_RCONTROL
)
921 ret
= VkToScan( Code
, FALSE
, keyLayout
);
926 DontDistinguishShifts
927 (IntMapVirtualKeyEx( Code
, 3, keyLayout
) );
934 ret
= VkToScan( Code
, FALSE
, keyLayout
);
935 ToUnicodeInner( Code
, ret
, 0, wp
, 2, 0, keyLayout
);
942 ret
= ScanToVk( Code
, FALSE
, keyLayout
);
951 NtUserMapVirtualKeyEx( UINT Code
, UINT Type
, DWORD keyboardId
, HKL dwhkl
)
953 PKBDTABLES keyLayout
= PsGetWin32Thread() ?
954 PsGetWin32Thread()->KeyboardLayout
: 0;
959 return IntMapVirtualKeyEx( Code
, Type
, keyLayout
);
974 BYTE KeyStateBuf
[0x100];
975 PWCHAR OutPwszBuff
= 0;
979 if( !NT_SUCCESS(MmCopyFromCaller(KeyStateBuf
,
981 sizeof(KeyStateBuf
))) )
983 DPRINT1( "Couldn't copy key state from caller.\n" );
986 OutPwszBuff
= ExAllocatePoolWithTag(NonPagedPool
,sizeof(WCHAR
) * cchBuff
, TAG_STRING
);
989 DPRINT1( "ExAllocatePool(%d) failed\n", sizeof(WCHAR
) * cchBuff
);
992 RtlZeroMemory( OutPwszBuff
, sizeof( WCHAR
) * cchBuff
);
994 ret
= ToUnicodeEx( wVirtKey
,
1002 MmCopyToCaller(pwszBuff
,OutPwszBuff
,sizeof(WCHAR
)*cchBuff
);
1003 ExFreePool(OutPwszBuff
);
1008 static int W32kSimpleToupper( int ch
)
1010 if( ch
>= 'a' && ch
<= 'z' )
1011 ch
= ch
- 'a' + 'A';
1017 NtUserGetKeyNameText( LONG lParam
, LPWSTR lpString
, int nSize
)
1023 UINT ScanCode
= (lParam
>> 16) & 0xff;
1024 BOOL ExtKey
= lParam
& (1<<24) ? TRUE
: FALSE
;
1025 PKBDTABLES keyLayout
=
1026 PsGetWin32Thread() ?
1027 PsGetWin32Thread()->KeyboardLayout
: 0;
1029 if( !keyLayout
|| nSize
< 1 )
1032 if( lParam
& (1<<25) )
1034 CareVk
= VkCode
= ScanToVk( ScanCode
, ExtKey
, keyLayout
);
1035 if( VkCode
== VK_LSHIFT
|| VkCode
== VK_RSHIFT
)
1037 if( VkCode
== VK_LCONTROL
|| VkCode
== VK_RCONTROL
)
1038 VkCode
= VK_LCONTROL
;
1039 if( VkCode
== VK_LMENU
|| VkCode
== VK_RMENU
)
1044 VkCode
= ScanToVk( ScanCode
, ExtKey
, keyLayout
);
1047 VSC_LPWSTR
*KeyNames
= 0;
1049 if( CareVk
!= VkCode
)
1050 ScanCode
= VkToScan( VkCode
, ExtKey
, keyLayout
);
1053 KeyNames
= keyLayout
->pKeyNamesExt
;
1055 KeyNames
= keyLayout
->pKeyNames
;
1057 for( i
= 0; KeyNames
[i
].pwsz
; i
++ )
1059 if( KeyNames
[i
].vsc
== ScanCode
)
1061 UINT StrLen
= wcslen(KeyNames
[i
].pwsz
);
1062 UINT StrMax
= StrLen
> (nSize
- 1) ? (nSize
- 1) : StrLen
;
1064 if( NT_SUCCESS( MmCopyToCaller( lpString
,
1066 StrMax
* sizeof(WCHAR
) ) ) &&
1067 NT_SUCCESS( MmCopyToCaller( lpString
+ StrMax
,
1069 sizeof( WCHAR
) ) ) )
1081 UCName
[0] = W32kSimpleToupper(IntMapVirtualKeyEx( VkCode
, 2, keyLayout
));
1085 if( !NT_SUCCESS(MmCopyToCaller( lpString
, UCName
, 2 * sizeof(WCHAR
) )) )
1093 * Filter this message according to the current key layout, setting wParam
1098 W32kKeyProcessMessage(LPMSG Msg
,
1099 PKBDTABLES KeyboardLayout
,
1102 DWORD ScanCode
= 0, ModifierBits
= 0;
1104 DWORD BaseMapping
= 0;
1106 static WORD NumpadConversion
[][2] =
1107 { { VK_DELETE
, VK_DECIMAL
},
1108 { VK_INSERT
, VK_NUMPAD0
},
1109 { VK_END
, VK_NUMPAD1
},
1110 { VK_DOWN
, VK_NUMPAD2
},
1111 { VK_NEXT
, VK_NUMPAD3
},
1112 { VK_LEFT
, VK_NUMPAD4
},
1113 { VK_CLEAR
, VK_NUMPAD5
},
1114 { VK_RIGHT
, VK_NUMPAD6
},
1115 { VK_HOME
, VK_NUMPAD7
},
1116 { VK_UP
, VK_NUMPAD8
},
1117 { VK_PRIOR
, VK_NUMPAD9
},
1119 PVSC_VK VscVkTable
= NULL
;
1121 if( !KeyboardLayout
|| !Msg
||
1122 (Msg
->message
!= WM_KEYDOWN
&& Msg
->message
!= WM_SYSKEYDOWN
&&
1123 Msg
->message
!= WM_KEYUP
&& Msg
->message
!= WM_SYSKEYUP
) )
1130 /* arty -- handle numpad -- On real windows, the actual key produced
1131 * by the messaging layer is different based on the state of numlock. */
1132 ModifierBits
= ModBits(KeyboardLayout
,QueueKeyStateTable
);
1134 /* Get the raw scan code, so we can look up whether the key is a numpad
1137 * Shift and the LP_EXT_BIT cancel. */
1138 ScanCode
= (Msg
->lParam
>> 16) & 0xff;
1139 BaseMapping
= Msg
->wParam
=
1140 IntMapVirtualKeyEx( ScanCode
, 1, KeyboardLayout
);
1143 if( ScanCode
>= KeyboardLayout
->bMaxVSCtoVK
)
1146 RawVk
= KeyboardLayout
->pusVSCtoVK
[ScanCode
];
1150 if( Prefix
== 0xE0 )
1152 /* ignore shift codes */
1153 if( ScanCode
== 0x2A || ScanCode
== 0x36 )
1155 IntUnLockQueueState
;
1158 VscVkTable
= KeyboardLayout
->pVSCtoVK_E0
;
1160 else if( Prefix
== 0xE1 )
1162 VscVkTable
= KeyboardLayout
->pVSCtoVK_E1
;
1166 while (VscVkTable
->Vsc
)
1168 if( VscVkTable
->Vsc
== ScanCode
)
1170 RawVk
= VscVkTable
->Vk
;
1176 if ((ModifierBits
& NUMLOCK_BIT
) &&
1177 !(ModifierBits
& GetShiftBit(KeyboardLayout
, VK_SHIFT
)) &&
1179 !(Msg
->lParam
& LP_EXT_BIT
))
1181 /* The key in question is a numpad key. Search for a translation. */
1182 for (i
= 0; NumpadConversion
[i
][0]; i
++)
1184 if ((BaseMapping
& 0xff) == NumpadConversion
[i
][0]) /* RawVk? */
1186 Msg
->wParam
= NumpadConversion
[i
][1];
1192 DPRINT("Key: [%04x -> %04x]\n", BaseMapping
, Msg
->wParam
);
1194 /* Now that we have the VK, we can set the keymap appropriately
1195 * This is a better place for this code, as it's guaranteed to be
1196 * run, unlike translate message. */
1197 if (Msg
->message
== WM_KEYDOWN
|| Msg
->message
== WM_SYSKEYDOWN
)
1199 SetKeyState( ScanCode
, Msg
->wParam
, Msg
->lParam
& LP_EXT_BIT
,
1200 TRUE
); /* Strike key */
1202 else if (Msg
->message
== WM_KEYUP
|| Msg
->message
== WM_SYSKEYUP
)
1204 SetKeyState( ScanCode
, Msg
->wParam
, Msg
->lParam
& LP_EXT_BIT
,
1205 FALSE
); /* Release key */
1208 /* We need to unset SYSKEYDOWN if the ALT key is an ALT+Gr */
1209 if( QueueKeyStateTable
[VK_RMENU
] & KS_DOWN_BIT
)
1211 if( Msg
->message
== WM_SYSKEYDOWN
)
1212 Msg
->message
= WM_KEYDOWN
;
1214 Msg
->message
= WM_KEYUP
;
1217 IntUnLockQueueState
;
1222 NtUserGetKeyboardLayoutList(
1233 NtUserGetKeyboardLayoutName(
1244 UserGetKeyboardLayout(
1249 PW32THREAD W32Thread
;
1253 W32Thread
= PsGetWin32Thread();
1256 Status
= PsLookupThreadByThreadId((HANDLE
)dwThreadId
, &Thread
);//fixme: deref thread
1257 if(!NT_SUCCESS(Status
))
1259 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
1262 W32Thread
= Thread
->Tcb
.Win32Thread
;
1264 layout
= W32Thread
->KeyboardLayout
;
1273 NtUserGetKeyboardLayout(
1276 DECLARE_RETURN(HKL
);
1279 DPRINT("Enter NtUserGetKeyboardLayout\n");
1281 RETURN( UserGetKeyboardLayout(dwThreadId
));
1284 DPRINT("Leave NtUserGetKeyboardLayout, ret=%i\n",_ret_
);
1291 UserGetKeyboardType(
1296 case 0: /* Keyboard type */
1297 return 4; /* AT-101 */
1298 case 1: /* Keyboard Subtype */
1299 return 0; /* There are no defined subtypes */
1300 case 2: /* Number of F-keys */
1301 return 12; /* We're doing an 101 for now, so return 12 F-keys */
1303 DPRINT1("Unknown type!\n");
1304 return 0; /* The book says 0 here, so 0 */
1310 NtUserGetKeyboardType(
1313 return UserGetKeyboardType(TypeFlag
);
1318 Based on TryToTranslateChar, instead of processing VirtualKey match,
1319 look for wChar match.
1325 DWORD KeyboardLayout
,
1328 PKBDTABLES KeyLayout
;
1329 PVK_TO_WCHAR_TABLE vtwTbl
;
1330 PVK_TO_WCHARS10 vkPtr
;
1331 size_t size_this_entry
;
1333 DWORD CapsMod
= 0, CapsState
= 0;
1337 KeyLayout
= (PKBDTABLES
) KeyboardLayout
;
1339 for (nMod
= 0; KeyLayout
->pVkToWcharTable
[nMod
].nModifications
; nMod
++)
1341 vtwTbl
= &KeyLayout
->pVkToWcharTable
[nMod
];
1342 size_this_entry
= vtwTbl
->cbSize
;
1343 vkPtr
= (PVK_TO_WCHARS10
)((BYTE
*)vtwTbl
->pVkToWchars
);
1345 while(vkPtr
->VirtualKey
)
1351 Should have only 8 valid possibilities. Including zero.
1353 for(CapsState
= 0; CapsState
< vtwTbl
->nModifications
; CapsState
++)
1355 if(vkPtr
->wch
[CapsState
] == wChar
)
1357 CapsMod
= KeyLayout
->pCharModifiers
->ModNumber
[CapsState
];
1358 DPRINT("nMod %d wC %04x: CapsMod %08x CapsState %08x MaxModBits %08x\n",
1359 nMod
, wChar
, CapsMod
, CapsState
, KeyLayout
->pCharModifiers
->wMaxModBits
);
1360 return ((CapsMod
<< 8)|(vkPtr
->VirtualKey
& 0xff));
1363 vkPtr
= (PVK_TO_WCHARS10
)(((BYTE
*)vkPtr
) + size_this_entry
);