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 */
53 BYTE gQueueKeyStateTable
[256];
56 /* FUNCTIONS *****************************************************************/
58 /* Initialization -- Right now, just zero the key state and init the lock */
59 NTSTATUS FASTCALL
InitKeyboardImpl(VOID
)
61 RtlZeroMemory(&gQueueKeyStateTable
,0x100);
62 return STATUS_SUCCESS
;
65 /*** Statics used by TranslateMessage ***/
67 /*** Shift state code was out of hand, sorry. --- arty */
69 static UINT
DontDistinguishShifts( UINT ret
)
71 if( ret
== VK_LSHIFT
|| ret
== VK_RSHIFT
)
73 if( ret
== VK_LCONTROL
|| ret
== VK_RCONTROL
)
75 if( ret
== VK_LMENU
|| ret
== VK_RMENU
)
80 static VOID STDCALL
SetKeyState(DWORD key
, DWORD vk
, DWORD ext
, BOOL down
)
84 /* Special handling for toggles like numpad and caps lock */
85 if (vk
== VK_CAPITAL
|| vk
== VK_NUMLOCK
)
88 gQueueKeyStateTable
[vk
] ^= KS_LOCK_BIT
;
91 if (ext
&& vk
== VK_LSHIFT
)
93 if (ext
&& vk
== VK_LCONTROL
)
95 if (ext
&& vk
== VK_LMENU
)
99 gQueueKeyStateTable
[vk
] |= KS_DOWN_BIT
;
101 gQueueKeyStateTable
[vk
] &= ~KS_DOWN_MASK
;
103 if (vk
== VK_LSHIFT
|| vk
== VK_RSHIFT
)
105 if ((gQueueKeyStateTable
[VK_LSHIFT
] & KS_DOWN_BIT
) ||
106 (gQueueKeyStateTable
[VK_RSHIFT
] & KS_DOWN_BIT
))
108 gQueueKeyStateTable
[VK_SHIFT
] |= KS_DOWN_BIT
;
112 gQueueKeyStateTable
[VK_SHIFT
] &= ~KS_DOWN_MASK
;
116 if (vk
== VK_LCONTROL
|| vk
== VK_RCONTROL
)
118 if ((gQueueKeyStateTable
[VK_LCONTROL
] & KS_DOWN_BIT
) ||
119 (gQueueKeyStateTable
[VK_RCONTROL
] & KS_DOWN_BIT
))
121 gQueueKeyStateTable
[VK_CONTROL
] |= KS_DOWN_BIT
;
125 gQueueKeyStateTable
[VK_CONTROL
] &= ~KS_DOWN_MASK
;
129 if (vk
== VK_LMENU
|| vk
== VK_RMENU
)
131 if ((gQueueKeyStateTable
[VK_LMENU
] & KS_DOWN_BIT
) ||
132 (gQueueKeyStateTable
[VK_RMENU
] & KS_DOWN_BIT
))
134 gQueueKeyStateTable
[VK_MENU
] |= KS_DOWN_BIT
;
138 gQueueKeyStateTable
[VK_MENU
] &= ~KS_DOWN_MASK
;
143 VOID
DumpKeyState( PBYTE KeyState
)
147 DbgPrint( "KeyState { " );
148 for( i
= 0; i
< 0x100; i
++ )
151 DbgPrint( "%02x(%02x) ", i
, KeyState
[i
] );
156 static BYTE
KeysSet( PKBDTABLES pkKT
, PBYTE KeyState
,
157 int FakeModLeft
, int FakeModRight
)
159 if( !KeyState
|| !pkKT
)
162 /* Search special codes first */
163 if( FakeModLeft
&& KeyState
[FakeModLeft
] )
164 return KeyState
[FakeModLeft
];
165 else if( FakeModRight
&& KeyState
[FakeModRight
] )
166 return KeyState
[FakeModRight
];
171 /* Search the keyboard layout modifiers table for the shift bit. I don't
172 * want to count on the shift bit not moving, because it can be specified
175 static DWORD FASTCALL
GetShiftBit( PKBDTABLES pkKT
, DWORD Vk
)
179 for( i
= 0; pkKT
->pCharModifiers
->pVkToBit
[i
].Vk
; i
++ )
180 if( pkKT
->pCharModifiers
->pVkToBit
[i
].Vk
== Vk
)
181 return pkKT
->pCharModifiers
->pVkToBit
[i
].ModBits
;
186 static DWORD
ModBits( PKBDTABLES pkKT
, PBYTE KeyState
)
193 /* DumpKeyState( KeyState ); */
195 if (KeysSet( pkKT
, KeyState
, VK_LSHIFT
, VK_RSHIFT
) &
197 ModBits
|= GetShiftBit( pkKT
, VK_SHIFT
);
199 if (KeysSet( pkKT
, KeyState
, VK_LCONTROL
, VK_RCONTROL
) &
201 ModBits
|= GetShiftBit( pkKT
, VK_CONTROL
);
203 if (KeysSet( pkKT
, KeyState
, VK_LMENU
, VK_RMENU
) &
205 ModBits
|= GetShiftBit( pkKT
, VK_MENU
);
208 if (KeysSet( pkKT
, KeyState
, VK_RMENU
, 0 ) &
210 ModBits
|= GetShiftBit( pkKT
, VK_CONTROL
);
212 /* Deal with VK_CAPITAL */
213 if (KeysSet( pkKT
, KeyState
, VK_CAPITAL
, 0 ) & KS_LOCK_BIT
)
215 ModBits
|= CAPITAL_BIT
;
218 /* Deal with VK_NUMLOCK */
219 if (KeysSet( pkKT
, KeyState
, VK_NUMLOCK
, 0 ) & KS_LOCK_BIT
)
221 ModBits
|= NUMLOCK_BIT
;
224 DPRINT( "Current Mod Bits: %x\n", ModBits
);
229 static BOOL
TryToTranslateChar(WORD wVirtKey
,
233 PWCHAR pwcTranslatedChar
,
234 PKBDTABLES keyLayout
)
236 PVK_TO_WCHAR_TABLE vtwTbl
;
237 PVK_TO_WCHARS10 vkPtr
;
238 size_t size_this_entry
;
240 DWORD CapsMod
= 0, CapsState
= 0;
242 CapsState
= ModBits
& ~MOD_BITS_MASK
;
243 ModBits
= ModBits
& MOD_BITS_MASK
;
245 DPRINT ( "TryToTranslate: %04x %x\n", wVirtKey
, ModBits
);
247 if (ModBits
> keyLayout
->pCharModifiers
->wMaxModBits
)
251 for (nMod
= 0; keyLayout
->pVkToWcharTable
[nMod
].nModifications
; nMod
++)
253 vtwTbl
= &keyLayout
->pVkToWcharTable
[nMod
];
254 size_this_entry
= vtwTbl
->cbSize
;
255 vkPtr
= (PVK_TO_WCHARS10
)((BYTE
*)vtwTbl
->pVkToWchars
);
256 while(vkPtr
->VirtualKey
)
258 if( wVirtKey
== (vkPtr
->VirtualKey
& 0xff) )
260 CapsMod
= keyLayout
->pCharModifiers
->ModNumber
262 ((CapsState
& CAPITAL_BIT
) ? vkPtr
->Attributes
: 0)];
264 if( CapsMod
> keyLayout
->pVkToWcharTable
[nMod
].nModifications
)
268 keyLayout
->pVkToWcharTable
[nMod
].nModifications
)
271 CapsMod
&= MaxBit
- 1; /* Guarantee that CapsMod lies
275 *pbDead
= vkPtr
->wch
[CapsMod
] == WCH_DEAD
;
276 *pbLigature
= vkPtr
->wch
[CapsMod
] == WCH_LGTR
;
277 *pwcTranslatedChar
= vkPtr
->wch
[CapsMod
];
279 DPRINT("%d %04x: CapsMod %08x CapsState %08x Char %04x\n",
281 CapsMod
, CapsState
, *pwcTranslatedChar
);
285 vkPtr
= (PVK_TO_WCHARS10
)(((BYTE
*)vkPtr
) + size_this_entry
);
286 if( vkPtr
->VirtualKey
!= 0xff )
288 DPRINT( "Found dead key with no trailer in the table.\n" );
289 DPRINT( "VK: %04x, ADDR: %08x\n", wVirtKey
, (int)vkPtr
);
292 *pwcTranslatedChar
= vkPtr
->wch
[CapsMod
];
296 vkPtr
= (PVK_TO_WCHARS10
)(((BYTE
*)vkPtr
) + size_this_entry
);
304 ToUnicodeInner(UINT wVirtKey
,
312 WCHAR wcTranslatedChar
;
319 if( TryToTranslateChar( wVirtKey
,
320 ModBits( pkKT
, lpKeyState
),
328 DPRINT("Not handling ligature (yet)\n" );
333 pwszBuff
[0] = wcTranslatedChar
;
335 return bDead
? -1 : 1;
342 DWORD FASTCALL
UserGetKeyState(DWORD key
)
348 ret
= ((DWORD
)(gQueueKeyStateTable
[key
] & KS_DOWN_BIT
) << 8 ) |
349 (gQueueKeyStateTable
[key
] & KS_LOCK_BIT
);
361 DECLARE_RETURN(DWORD
);
363 DPRINT("Enter NtUserGetKeyState\n");
364 UserEnterExclusive();
366 RETURN(UserGetKeyState(key
));
369 DPRINT("Leave NtUserGetKeyState, ret=%i\n",_ret_
);
376 DWORD FASTCALL
UserGetAsyncKeyState(DWORD key
)
382 ret
= ((DWORD
)(gQueueKeyStateTable
[key
] & KS_DOWN_BIT
) << 8 ) |
383 (gQueueKeyStateTable
[key
] & KS_LOCK_BIT
);
393 NtUserGetAsyncKeyState(
396 DECLARE_RETURN(DWORD
);
398 DPRINT("Enter NtUserGetAsyncKeyState\n");
399 UserEnterExclusive();
401 RETURN(UserGetAsyncKeyState(key
));
404 DPRINT("Leave NtUserGetAsyncKeyState, ret=%i\n",_ret_
);
411 int STDCALL
ToUnicodeEx( UINT wVirtKey
,
419 int ToUnicodeResult
= 0;
421 if (0 == (lpKeyState
[wVirtKey
] & KS_DOWN_BIT
))
427 ToUnicodeResult
= ToUnicodeInner( wVirtKey
,
434 PsGetWin32Thread()->KeyboardLayout
: 0 );
437 return ToUnicodeResult
;
440 int STDCALL
ToUnicode( UINT wVirtKey
,
447 return ToUnicodeEx( wVirtKey
,
457 * Utility to copy and append two unicode strings.
459 * IN OUT PUNICODE_STRING ResultFirst -> First string and result
460 * IN PUNICODE_STRING Second -> Second string to append
461 * IN BOOL Deallocate -> TRUE: Deallocate First string before
467 NTSTATUS NTAPI
AppendUnicodeString(PUNICODE_STRING ResultFirst
,
468 PUNICODE_STRING Second
,
473 ExAllocatePoolWithTag(PagedPool
,
474 (ResultFirst
->Length
+ Second
->Length
+ sizeof(WCHAR
)),
478 return STATUS_NO_MEMORY
;
480 memcpy( new_string
, ResultFirst
->Buffer
,
481 ResultFirst
->Length
);
482 memcpy( new_string
+ ResultFirst
->Length
/ sizeof(WCHAR
),
486 RtlFreeUnicodeString(ResultFirst
);
487 ResultFirst
->Length
+= Second
->Length
;
488 ResultFirst
->MaximumLength
= ResultFirst
->Length
;
489 new_string
[ResultFirst
->Length
/ sizeof(WCHAR
)] = 0;
490 Status
= RtlCreateUnicodeString(ResultFirst
,new_string
) ?
491 STATUS_SUCCESS
: STATUS_NO_MEMORY
;
492 ExFreePool(new_string
);
497 * Utility function to read a value from the registry more easily.
499 * IN PUNICODE_STRING KeyName -> Name of key to open
500 * IN PUNICODE_STRING ValueName -> Name of value to open
501 * OUT PUNICODE_STRING ReturnedValue -> String contained in registry
506 static NTSTATUS NTAPI
ReadRegistryValue( PUNICODE_STRING KeyName
,
507 PUNICODE_STRING ValueName
,
508 PUNICODE_STRING ReturnedValue
)
512 OBJECT_ATTRIBUTES KeyAttributes
;
513 PKEY_VALUE_PARTIAL_INFORMATION KeyValuePartialInfo
;
518 InitializeObjectAttributes(&KeyAttributes
, KeyName
, OBJ_CASE_INSENSITIVE
,
520 Status
= ZwOpenKey(&KeyHandle
, KEY_ALL_ACCESS
, &KeyAttributes
);
521 if( !NT_SUCCESS(Status
) )
526 Status
= ZwQueryValueKey(KeyHandle
, ValueName
, KeyValuePartialInformation
,
531 if( Status
!= STATUS_BUFFER_TOO_SMALL
)
537 ResLength
+= sizeof( *KeyValuePartialInfo
);
538 KeyValuePartialInfo
=
539 ExAllocatePoolWithTag(PagedPool
, ResLength
, TAG_STRING
);
542 if( !KeyValuePartialInfo
)
545 return STATUS_NO_MEMORY
;
548 Status
= ZwQueryValueKey(KeyHandle
, ValueName
, KeyValuePartialInformation
,
549 (PVOID
)KeyValuePartialInfo
,
553 if( !NT_SUCCESS(Status
) )
556 ExFreePool(KeyValuePartialInfo
);
560 Temp
.Length
= Temp
.MaximumLength
= KeyValuePartialInfo
->DataLength
;
561 Temp
.Buffer
= (PWCHAR
)KeyValuePartialInfo
->Data
;
563 /* At this point, KeyValuePartialInfo->Data contains the key data */
564 RtlInitUnicodeString(ReturnedValue
,L
"");
565 AppendUnicodeString(ReturnedValue
,&Temp
,FALSE
);
567 ExFreePool(KeyValuePartialInfo
);
573 typedef PVOID (*KbdLayerDescriptor
)(VOID
);
574 NTSTATUS STDCALL
LdrGetProcedureAddress(PVOID module
,
575 PANSI_STRING import_name
,
579 void InitKbdLayout( PVOID
*pkKeyboardLayout
)
581 WCHAR LocaleBuffer
[16];
582 UNICODE_STRING LayoutKeyName
;
583 UNICODE_STRING LayoutValueName
;
584 UNICODE_STRING DefaultLocale
;
585 UNICODE_STRING LayoutFile
;
586 UNICODE_STRING FullLayoutPath
;
588 PWCHAR KeyboardLayoutWSTR
;
589 HMODULE kbModule
= 0;
591 ANSI_STRING kbdProcedureName
;
592 KbdLayerDescriptor layerDescGetFn
;
594 #define XX_STATUS(x) if (!NT_SUCCESS(Status = (x))) continue;
598 Status
= ZwQueryDefaultLocale(FALSE
, &LocaleId
);
599 if (!NT_SUCCESS(Status
))
601 DPRINT1("Could not get default locale (%08lx).\n", Status
);
605 DPRINT("DefaultLocale = %lx\n", LocaleId
);
606 swprintf(LocaleBuffer
, L
"%08lx", LocaleId
);
607 DPRINT("DefaultLocale = %S\n", LocaleBuffer
);
608 RtlInitUnicodeString(&DefaultLocale
, LocaleBuffer
);
610 RtlInitUnicodeString(&LayoutKeyName
,
611 L
"\\REGISTRY\\Machine\\SYSTEM\\CurrentControlSet"
612 L
"\\Control\\KeyboardLayouts\\");
614 AppendUnicodeString(&LayoutKeyName
,&DefaultLocale
,FALSE
);
616 RtlInitUnicodeString(&LayoutValueName
,L
"Layout File");
618 Status
= ReadRegistryValue(&LayoutKeyName
,&LayoutValueName
,&LayoutFile
);
619 RtlInitUnicodeString(&FullLayoutPath
,SYSTEMROOT_DIR
);
621 if( !NT_SUCCESS(Status
) )
623 DPRINT1("Got default locale but not layout file. (%08lx)\n",
628 DPRINT("Read registry and got %wZ\n", &LayoutFile
);
630 RtlFreeUnicodeString(&LayoutKeyName
);
632 AppendUnicodeString(&FullLayoutPath
,&LayoutFile
,FALSE
);
634 DPRINT("Loading Keyboard DLL %wZ\n", &FullLayoutPath
);
636 RtlFreeUnicodeString(&LayoutFile
);
639 ExAllocatePoolWithTag(PagedPool
,
640 FullLayoutPath
.Length
+ sizeof(WCHAR
),
643 if( !KeyboardLayoutWSTR
)
645 DPRINT1("Couldn't allocate a string for the keyboard layout name.\n");
646 RtlFreeUnicodeString(&FullLayoutPath
);
649 memcpy(KeyboardLayoutWSTR
,FullLayoutPath
.Buffer
,
650 FullLayoutPath
.Length
+ sizeof(WCHAR
));
651 KeyboardLayoutWSTR
[FullLayoutPath
.Length
/ sizeof(WCHAR
)] = 0;
653 kbModule
= EngLoadImage(KeyboardLayoutWSTR
);
654 DPRINT( "Load Keyboard Layout: %S\n", KeyboardLayoutWSTR
);
657 DPRINT1( "Load Keyboard Layout: No %wZ\n", &FullLayoutPath
);
659 RtlFreeUnicodeString(&FullLayoutPath
);
665 DPRINT1("Trying to load US Keyboard Layout\n");
666 kbModule
= EngLoadImage(L
"\\SystemRoot\\system32\\kbdus.dll");
670 DPRINT1("Failed to load any Keyboard Layout\n");
675 RtlInitAnsiString( &kbdProcedureName
, "KbdLayerDescriptor" );
677 LdrGetProcedureAddress((PVOID
)kbModule
,
680 (PVOID
*)&layerDescGetFn
);
684 *pkKeyboardLayout
= layerDescGetFn();
689 if( !*pkKeyboardLayout
)
691 DPRINT1("Failed to load the keyboard layout.\n");
697 PKBDTABLES
W32kGetDefaultKeyLayout()
699 PKBDTABLES pkKeyboardLayout
= 0;
700 InitKbdLayout( (PVOID
) &pkKeyboardLayout
);
701 return pkKeyboardLayout
;
705 IntTranslateKbdMessage(LPMSG lpMsg
,
708 static INT dead_char
= 0;
712 PKBDTABLES keyLayout
;
717 keyLayout
= PsGetWin32Thread()->KeyboardLayout
;
721 if (lpMsg
->message
!= WM_KEYDOWN
&& lpMsg
->message
!= WM_SYSKEYDOWN
)
724 ScanCode
= (lpMsg
->lParam
>> 16) & 0xff;
726 /* All messages have to contain the cursor point. */
727 IntGetCursorLocation(PsGetWin32Thread()->Desktop
->WindowStation
,
730 UState
= ToUnicodeInner(lpMsg
->wParam
, HIWORD(lpMsg
->lParam
) & 0xff,
731 gQueueKeyStateTable
, wp
, 2, 0,
736 NewMsg
.message
= (lpMsg
->message
== WM_KEYDOWN
) ? WM_CHAR
: WM_SYSCHAR
;
741 DPRINT("PREVIOUS DEAD CHAR: %c\n", dead_char
);
743 for( i
= 0; keyLayout
->pDeadKey
[i
].dwBoth
; i
++ )
745 first
= keyLayout
->pDeadKey
[i
].dwBoth
>> 16;
746 second
= keyLayout
->pDeadKey
[i
].dwBoth
;
747 if (first
== dead_char
&& second
== wp
[0])
749 wp
[0] = keyLayout
->pDeadKey
[i
].wchComposed
;
755 DPRINT("FINAL CHAR: %c\n", wp
[0]);
760 NewMsg
.hwnd
= lpMsg
->hwnd
;
761 NewMsg
.wParam
= dead_char
;
762 NewMsg
.lParam
= lpMsg
->lParam
;
764 MsqPostMessage(PsGetWin32Thread()->MessageQueue
, &NewMsg
, FALSE
, QS_KEY
);
767 NewMsg
.hwnd
= lpMsg
->hwnd
;
768 NewMsg
.wParam
= wp
[0];
769 NewMsg
.lParam
= lpMsg
->lParam
;
770 DPRINT( "CHAR='%c' %04x %08x\n", wp
[0], wp
[0], lpMsg
->lParam
);
771 MsqPostMessage(PsGetWin32Thread()->MessageQueue
, &NewMsg
, FALSE
, QS_KEY
);
774 else if (UState
== -1)
777 (lpMsg
->message
== WM_KEYDOWN
) ? WM_DEADCHAR
: WM_SYSDEADCHAR
;
778 NewMsg
.hwnd
= lpMsg
->hwnd
;
779 NewMsg
.wParam
= wp
[0];
780 NewMsg
.lParam
= lpMsg
->lParam
;
782 MsqPostMessage(PsGetWin32Thread()->MessageQueue
, &NewMsg
, FALSE
, QS_KEY
);
791 NtUserGetKeyboardState(
795 DECLARE_RETURN(DWORD
);
797 DPRINT("Enter NtUserGetKeyboardState\n");
802 if(!NT_SUCCESS(MmCopyToCaller(lpKeyState
, gQueueKeyStateTable
, 256)))
809 DPRINT("Leave NtUserGetKeyboardState, ret=%i\n",_ret_
);
816 NtUserSetKeyboardState(LPBYTE lpKeyState
)
819 DECLARE_RETURN(DWORD
);
821 DPRINT("Enter NtUserSetKeyboardState\n");
822 UserEnterExclusive();
826 if(! NT_SUCCESS(MmCopyFromCaller(gQueueKeyStateTable
, lpKeyState
, 256)))
833 DPRINT("Leave NtUserSetKeyboardState, ret=%i\n",_ret_
);
838 static UINT
VkToScan( UINT Code
, BOOL ExtCode
, PKBDTABLES pkKT
)
842 for( i
= 0; i
< pkKT
->bMaxVSCtoVK
; i
++ )
844 if( pkKT
->pusVSCtoVK
[i
] == Code
)
853 UINT
ScanToVk( UINT Code
, BOOL ExtKey
, PKBDTABLES pkKT
)
857 DPRINT("ScanToVk: No layout\n");
865 for( i
= 0; pkKT
->pVSCtoVK_E0
[i
].Vsc
; i
++ )
867 if( pkKT
->pVSCtoVK_E0
[i
].Vsc
== Code
)
868 return pkKT
->pVSCtoVK_E0
[i
].Vk
& 0xff;
870 for( i
= 0; pkKT
->pVSCtoVK_E1
[i
].Vsc
; i
++ )
872 if( pkKT
->pVSCtoVK_E1
[i
].Vsc
== Code
)
873 return pkKT
->pVSCtoVK_E1
[i
].Vk
& 0xff;
880 if( Code
>= pkKT
->bMaxVSCtoVK
)
884 return pkKT
->pusVSCtoVK
[Code
] & 0xff;
889 * Map a virtual key code, or virtual scan code, to a scan code, key code,
890 * or unshifted unicode character.
894 * 0 -- Code is a virtual key code that is converted into a virtual scan code
895 * that does not distinguish between left and right shift keys.
896 * 1 -- Code is a virtual scan code that is converted into a virtual key code
897 * that does not distinguish between left and right shift keys.
898 * 2 -- Code is a virtual key code that is converted into an unshifted unicode
900 * 3 -- Code is a virtual scan code that is converted into a virtual key code
901 * that distinguishes left and right shift keys.
902 * KeyLayout: Keyboard layout handle (currently, unused)
907 static UINT
IntMapVirtualKeyEx( UINT Code
, UINT Type
, PKBDTABLES keyLayout
)
914 if( Code
== VK_RSHIFT
)
916 if( Code
== VK_RMENU
)
918 if( Code
== VK_RCONTROL
)
920 ret
= VkToScan( Code
, FALSE
, keyLayout
);
925 DontDistinguishShifts
926 (IntMapVirtualKeyEx( Code
, 3, keyLayout
) );
933 ret
= VkToScan( Code
, FALSE
, keyLayout
);
934 ToUnicodeInner( Code
, ret
, 0, wp
, 2, 0, keyLayout
);
941 ret
= ScanToVk( Code
, FALSE
, keyLayout
);
950 NtUserMapVirtualKeyEx( UINT Code
, UINT Type
, DWORD keyboardId
, HKL dwhkl
)
952 PKBDTABLES keyLayout
;
953 DECLARE_RETURN(UINT
);
955 DPRINT("Enter NtUserMapVirtualKeyEx\n");
956 UserEnterExclusive();
958 keyLayout
= PsGetWin32Thread() ? PsGetWin32Thread()->KeyboardLayout
: 0;
963 RETURN(IntMapVirtualKeyEx( Code
, Type
, keyLayout
));
966 DPRINT("Leave NtUserMapVirtualKeyEx, ret=%i\n",_ret_
);
983 BYTE KeyStateBuf
[0x100];
984 PWCHAR OutPwszBuff
= 0;
988 DPRINT("Enter NtUserSetKeyboardState\n");
989 UserEnterShared();//faxme: this syscall doesnt seem to need any locking...
992 if( !NT_SUCCESS(MmCopyFromCaller(KeyStateBuf
,
994 sizeof(KeyStateBuf
))) )
996 DPRINT1( "Couldn't copy key state from caller.\n" );
999 OutPwszBuff
= ExAllocatePoolWithTag(NonPagedPool
,sizeof(WCHAR
) * cchBuff
, TAG_STRING
);
1002 DPRINT1( "ExAllocatePool(%d) failed\n", sizeof(WCHAR
) * cchBuff
);
1005 RtlZeroMemory( OutPwszBuff
, sizeof( WCHAR
) * cchBuff
);
1007 ret
= ToUnicodeEx( wVirtKey
,
1015 MmCopyToCaller(pwszBuff
,OutPwszBuff
,sizeof(WCHAR
)*cchBuff
);
1016 ExFreePool(OutPwszBuff
);
1021 DPRINT("Leave NtUserSetKeyboardState, ret=%i\n",_ret_
);
1026 static int W32kSimpleToupper( int ch
)
1028 if( ch
>= 'a' && ch
<= 'z' )
1029 ch
= ch
- 'a' + 'A';
1035 NtUserGetKeyNameText( LONG lParam
, LPWSTR lpString
, int nSize
)
1041 UINT ScanCode
= (lParam
>> 16) & 0xff;
1042 BOOL ExtKey
= lParam
& (1<<24) ? TRUE
: FALSE
;
1043 PKBDTABLES keyLayout
;
1044 DECLARE_RETURN(DWORD
);
1046 DPRINT("Enter NtUserGetKeyNameText\n");
1049 keyLayout
= PsGetWin32Thread() ?
1050 PsGetWin32Thread()->KeyboardLayout
: 0;
1052 if( !keyLayout
|| nSize
< 1 )
1055 if( lParam
& (1<<25) )
1057 CareVk
= VkCode
= ScanToVk( ScanCode
, ExtKey
, keyLayout
);
1058 if( VkCode
== VK_LSHIFT
|| VkCode
== VK_RSHIFT
)
1060 if( VkCode
== VK_LCONTROL
|| VkCode
== VK_RCONTROL
)
1061 VkCode
= VK_LCONTROL
;
1062 if( VkCode
== VK_LMENU
|| VkCode
== VK_RMENU
)
1067 VkCode
= ScanToVk( ScanCode
, ExtKey
, keyLayout
);
1070 VSC_LPWSTR
*KeyNames
= 0;
1072 if( CareVk
!= VkCode
)
1073 ScanCode
= VkToScan( VkCode
, ExtKey
, keyLayout
);
1076 KeyNames
= keyLayout
->pKeyNamesExt
;
1078 KeyNames
= keyLayout
->pKeyNames
;
1080 for( i
= 0; KeyNames
[i
].pwsz
; i
++ )
1082 if( KeyNames
[i
].vsc
== ScanCode
)
1084 UINT StrLen
= wcslen(KeyNames
[i
].pwsz
);
1085 UINT StrMax
= StrLen
> (nSize
- 1) ? (nSize
- 1) : StrLen
;
1087 if( NT_SUCCESS( MmCopyToCaller( lpString
,
1089 StrMax
* sizeof(WCHAR
) ) ) &&
1090 NT_SUCCESS( MmCopyToCaller( lpString
+ StrMax
,
1092 sizeof( WCHAR
) ) ) )
1104 UCName
[0] = W32kSimpleToupper(IntMapVirtualKeyEx( VkCode
, 2, keyLayout
));
1108 if( !NT_SUCCESS(MmCopyToCaller( lpString
, UCName
, 2 * sizeof(WCHAR
) )) )
1115 DPRINT("Leave NtUserGetKeyNameText, ret=%i\n",_ret_
);
1121 * Filter this message according to the current key layout, setting wParam
1126 W32kKeyProcessMessage(LPMSG Msg
,
1127 PKBDTABLES KeyboardLayout
,
1130 DWORD ScanCode
= 0, ModifierBits
= 0;
1132 DWORD BaseMapping
= 0;
1134 static WORD NumpadConversion
[][2] =
1135 { { VK_DELETE
, VK_DECIMAL
},
1136 { VK_INSERT
, VK_NUMPAD0
},
1137 { VK_END
, VK_NUMPAD1
},
1138 { VK_DOWN
, VK_NUMPAD2
},
1139 { VK_NEXT
, VK_NUMPAD3
},
1140 { VK_LEFT
, VK_NUMPAD4
},
1141 { VK_CLEAR
, VK_NUMPAD5
},
1142 { VK_RIGHT
, VK_NUMPAD6
},
1143 { VK_HOME
, VK_NUMPAD7
},
1144 { VK_UP
, VK_NUMPAD8
},
1145 { VK_PRIOR
, VK_NUMPAD9
},
1147 PVSC_VK VscVkTable
= NULL
;
1149 if( !KeyboardLayout
|| !Msg
||
1150 (Msg
->message
!= WM_KEYDOWN
&& Msg
->message
!= WM_SYSKEYDOWN
&&
1151 Msg
->message
!= WM_KEYUP
&& Msg
->message
!= WM_SYSKEYUP
) )
1156 /* arty -- handle numpad -- On real windows, the actual key produced
1157 * by the messaging layer is different based on the state of numlock. */
1158 ModifierBits
= ModBits(KeyboardLayout
,gQueueKeyStateTable
);
1160 /* Get the raw scan code, so we can look up whether the key is a numpad
1163 * Shift and the LP_EXT_BIT cancel. */
1164 ScanCode
= (Msg
->lParam
>> 16) & 0xff;
1165 BaseMapping
= Msg
->wParam
=
1166 IntMapVirtualKeyEx( ScanCode
, 1, KeyboardLayout
);
1169 if( ScanCode
>= KeyboardLayout
->bMaxVSCtoVK
)
1172 RawVk
= KeyboardLayout
->pusVSCtoVK
[ScanCode
];
1176 if( Prefix
== 0xE0 )
1178 /* ignore shift codes */
1179 if( ScanCode
== 0x2A || ScanCode
== 0x36 )
1183 VscVkTable
= KeyboardLayout
->pVSCtoVK_E0
;
1185 else if( Prefix
== 0xE1 )
1187 VscVkTable
= KeyboardLayout
->pVSCtoVK_E1
;
1191 while (VscVkTable
->Vsc
)
1193 if( VscVkTable
->Vsc
== ScanCode
)
1195 RawVk
= VscVkTable
->Vk
;
1201 if ((ModifierBits
& NUMLOCK_BIT
) &&
1202 !(ModifierBits
& GetShiftBit(KeyboardLayout
, VK_SHIFT
)) &&
1204 !(Msg
->lParam
& LP_EXT_BIT
))
1206 /* The key in question is a numpad key. Search for a translation. */
1207 for (i
= 0; NumpadConversion
[i
][0]; i
++)
1209 if ((BaseMapping
& 0xff) == NumpadConversion
[i
][0]) /* RawVk? */
1211 Msg
->wParam
= NumpadConversion
[i
][1];
1217 DPRINT("Key: [%04x -> %04x]\n", BaseMapping
, Msg
->wParam
);
1219 /* Now that we have the VK, we can set the keymap appropriately
1220 * This is a better place for this code, as it's guaranteed to be
1221 * run, unlike translate message. */
1222 if (Msg
->message
== WM_KEYDOWN
|| Msg
->message
== WM_SYSKEYDOWN
)
1224 SetKeyState( ScanCode
, Msg
->wParam
, Msg
->lParam
& LP_EXT_BIT
,
1225 TRUE
); /* Strike key */
1227 else if (Msg
->message
== WM_KEYUP
|| Msg
->message
== WM_SYSKEYUP
)
1229 SetKeyState( ScanCode
, Msg
->wParam
, Msg
->lParam
& LP_EXT_BIT
,
1230 FALSE
); /* Release key */
1233 /* We need to unset SYSKEYDOWN if the ALT key is an ALT+Gr */
1234 if( gQueueKeyStateTable
[VK_RMENU
] & KS_DOWN_BIT
)
1236 if( Msg
->message
== WM_SYSKEYDOWN
)
1237 Msg
->message
= WM_KEYDOWN
;
1239 Msg
->message
= WM_KEYUP
;
1246 NtUserGetKeyboardLayoutList(
1257 NtUserGetKeyboardLayoutName(
1268 UserGetKeyboardLayout(
1273 PW32THREAD W32Thread
;
1277 W32Thread
= PsGetWin32Thread();
1280 Status
= PsLookupThreadByThreadId((HANDLE
)dwThreadId
, &Thread
);//fixme: deref thread
1281 if(!NT_SUCCESS(Status
))
1283 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
1286 W32Thread
= Thread
->Tcb
.Win32Thread
;
1288 layout
= W32Thread
->KeyboardLayout
;
1297 NtUserGetKeyboardLayout(
1300 DECLARE_RETURN(HKL
);
1303 DPRINT("Enter NtUserGetKeyboardLayout\n");
1305 RETURN( UserGetKeyboardLayout(dwThreadId
));
1308 DPRINT("Leave NtUserGetKeyboardLayout, ret=%i\n",_ret_
);
1315 UserGetKeyboardType(
1320 case 0: /* Keyboard type */
1321 return 4; /* AT-101 */
1322 case 1: /* Keyboard Subtype */
1323 return 0; /* There are no defined subtypes */
1324 case 2: /* Number of F-keys */
1325 return 12; /* We're doing an 101 for now, so return 12 F-keys */
1327 DPRINT1("Unknown type!\n");
1328 return 0; /* The book says 0 here, so 0 */
1334 NtUserGetKeyboardType(
1337 DECLARE_RETURN(DWORD
);
1339 DPRINT("Enter NtUserGetKeyboardType\n");
1342 RETURN( UserGetKeyboardType(TypeFlag
));
1345 DPRINT("Leave NtUserGetKeyboardType, ret=%i\n",_ret_
);
1352 Based on TryToTranslateChar, instead of processing VirtualKey match,
1353 look for wChar match.
1359 DWORD KeyboardLayout
,
1362 /* FAXME: currently, this routine doesnt seem to need any locking */
1364 PKBDTABLES KeyLayout
;
1365 PVK_TO_WCHAR_TABLE vtwTbl
;
1366 PVK_TO_WCHARS10 vkPtr
;
1367 size_t size_this_entry
;
1369 DWORD CapsMod
= 0, CapsState
= 0;
1373 KeyLayout
= (PKBDTABLES
) KeyboardLayout
;
1375 for (nMod
= 0; KeyLayout
->pVkToWcharTable
[nMod
].nModifications
; nMod
++)
1377 vtwTbl
= &KeyLayout
->pVkToWcharTable
[nMod
];
1378 size_this_entry
= vtwTbl
->cbSize
;
1379 vkPtr
= (PVK_TO_WCHARS10
)((BYTE
*)vtwTbl
->pVkToWchars
);
1381 while(vkPtr
->VirtualKey
)
1387 Should have only 8 valid possibilities. Including zero.
1389 for(CapsState
= 0; CapsState
< vtwTbl
->nModifications
; CapsState
++)
1391 if(vkPtr
->wch
[CapsState
] == wChar
)
1393 CapsMod
= KeyLayout
->pCharModifiers
->ModNumber
[CapsState
];
1394 DPRINT("nMod %d wC %04x: CapsMod %08x CapsState %08x MaxModBits %08x\n",
1395 nMod
, wChar
, CapsMod
, CapsState
, KeyLayout
->pCharModifiers
->wMaxModBits
);
1396 return ((CapsMod
<< 8)|(vkPtr
->VirtualKey
& 0xff));
1399 vkPtr
= (PVK_TO_WCHARS10
)(((BYTE
*)vkPtr
) + size_this_entry
);