3 * Copyright (C) 1998, 1999, 2000, 2001 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 * PROJECT: ReactOS user32.dll
21 * FILE: dll/win32/user32/windows/input.c
23 * PROGRAMMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
25 * 09-05-2001 CSH Created
28 /* INCLUDES ******************************************************************/
32 #include <wine/debug.h>
33 WINE_DEFAULT_DEBUG_CHANNEL(user32
);
35 /* GLOBALS *******************************************************************/
39 /* FUNCTIONS *****************************************************************/
52 return NtUserDragDetect(hWnd
, pt
);
57 ULONG dx
= GetSystemMetrics(SM_CXDRAG
);
58 ULONG dy
= GetSystemMetrics(SM_CYDRAG
);
60 rect
.left
= pt
.x
- dx
;
61 rect
.right
= pt
.x
+ dx
;
63 rect
.bottom
= pt
.y
+ dy
;
70 PeekMessageW(&msg
, 0, WM_MOUSEFIRST
, WM_MOUSELAST
, PM_REMOVE
) ||
71 PeekMessageW(&msg
, 0, WM_KEYFIRST
, WM_KEYLAST
, PM_REMOVE
)
74 if (msg
.message
== WM_LBUTTONUP
)
79 if (msg
.message
== WM_MOUSEMOVE
)
81 tmp
.x
= LOWORD(msg
.lParam
);
82 tmp
.y
= HIWORD(msg
.lParam
);
83 if (!PtInRect(&rect
, tmp
))
89 if (msg
.message
== WM_KEYDOWN
)
91 if (msg
.wParam
== VK_ESCAPE
)
109 EnableWindow(HWND hWnd
,
112 // This will soon be moved to win32k.
114 LONG Style
= GetWindowLongPtrW(hWnd
, GWL_STYLE
);
115 /* check if updating is needed */
116 UINT bIsDisabled
= (Style
& WS_DISABLED
);
117 Update
= bIsDisabled
;
121 Style
&= ~WS_DISABLED
;
125 Update
= !bIsDisabled
;
127 SendMessageW( hWnd
, WM_CANCELMODE
, 0, 0);
129 /* Remove keyboard focus from that window if it had focus */
130 if (hWnd
== GetFocus())
134 Style
|= WS_DISABLED
;
137 NtUserSetWindowLong(hWnd
, GWL_STYLE
, Style
, FALSE
);
141 IntNotifyWinEvent(EVENT_OBJECT_STATECHANGE
, hWnd
, OBJID_WINDOW
, CHILDID_SELF
, 0);
142 SendMessageW(hWnd
, WM_ENABLE
, (LPARAM
)bEnable
, 0);
144 // Return nonzero if it was disabled, or zero if it wasn't:
153 GetAsyncKeyState(int vKey
)
155 if (vKey
< 0 || vKey
> 256)
157 return (SHORT
) NtUserGetAsyncKeyState((DWORD
) vKey
);
165 GetKeyboardLayout(DWORD idThread
)
167 return (HKL
)NtUserCallOneParam((DWORD_PTR
) idThread
, ONEPARAM_ROUTINE_GETKEYBOARDLAYOUT
);
185 GetKeyNameTextA(LONG lParam
,
189 LPWSTR intermediateString
=
190 HeapAlloc(GetProcessHeap(),0,nSize
* sizeof(WCHAR
));
193 BOOL defChar
= FALSE
;
195 if( !intermediateString
) return 0;
196 ret
= GetKeyNameTextW(lParam
,intermediateString
,nSize
);
197 if( ret
== 0 ) { lpString
[0] = 0; return 0; }
199 wstrLen
= wcslen( intermediateString
);
200 ret
= WideCharToMultiByte(CP_ACP
, 0,
201 intermediateString
, wstrLen
,
202 lpString
, nSize
, ".", &defChar
);
204 HeapFree(GetProcessHeap(),0,intermediateString
);
213 GetKeyNameTextW(LONG lParam
,
217 return NtUserGetKeyNameText( lParam
, lpString
, nSize
);
225 GetKeyState(int nVirtKey
)
227 return (SHORT
) NtUserGetKeyState((DWORD
) nVirtKey
);
235 GetKeyboardLayoutNameA(LPSTR pwszKLID
)
237 WCHAR buf
[KL_NAMELENGTH
];
239 if (GetKeyboardLayoutNameW(buf
))
240 return WideCharToMultiByte( CP_ACP
, 0, buf
, -1, pwszKLID
, KL_NAMELENGTH
, NULL
, NULL
) != 0;
249 GetKeyboardLayoutNameW(LPWSTR pwszKLID
)
251 return NtUserGetKeyboardLayoutName( pwszKLID
);
259 GetKeyboardType(int nTypeFlag
)
261 return (int)NtUserCallOneParam((DWORD_PTR
) nTypeFlag
, ONEPARAM_ROUTINE_GETKEYBOARDTYPE
);
269 GetLastInputInfo(PLASTINPUTINFO plii
)
271 return NtUserGetLastInputInfo(plii
);
279 LoadKeyboardLayoutA(LPCSTR pwszKLID
,
282 return NtUserLoadKeyboardLayoutEx( NULL
, 0, NULL
, NULL
, NULL
,
283 strtoul(pwszKLID
, NULL
, 16),
292 LoadKeyboardLayoutW(LPCWSTR pwszKLID
,
295 // Look at revision 25596 to see how it's done in windows.
296 // We will do things our own way. Also be compatible too!
297 return NtUserLoadKeyboardLayoutEx( NULL
, 0, NULL
, NULL
, NULL
,
298 wcstoul(pwszKLID
, NULL
, 16),
307 MapVirtualKeyA(UINT uCode
,
310 return MapVirtualKeyExA( uCode
, uMapType
, GetKeyboardLayout( 0 ) );
318 MapVirtualKeyExA(UINT uCode
,
322 return MapVirtualKeyExW( uCode
, uMapType
, dwhkl
);
330 MapVirtualKeyExW(UINT uCode
,
334 return NtUserMapVirtualKeyEx( uCode
, uMapType
, 0, dwhkl
);
342 MapVirtualKeyW(UINT uCode
,
345 return MapVirtualKeyExW( uCode
, uMapType
, GetKeyboardLayout( 0 ) );
353 OemKeyScan(WORD wOemChar
)
359 MultiByteToWideChar(CP_OEMCP
, 0, (PCSTR
)&wOemChar
, 1, &p
, 1);
361 Scan
= MapVirtualKeyW((Vk
& 0x00ff), 0);
364 Page 450-1, MS W2k SuperBible by SAMS. Return, low word has the
365 scan code and high word has the shift state.
367 return ((Vk
& 0xff00) << 8) | Scan
;
375 SetDoubleClickTime(UINT uInterval
)
377 return (BOOL
)NtUserSystemParametersInfo(SPI_SETDOUBLECLICKTIME
,
392 return NtUserSwapMouseButton(fSwap
);
400 ToAscii(UINT uVirtKey
,
402 CONST BYTE
*lpKeyState
,
406 return ToAsciiEx(uVirtKey
, uScanCode
, lpKeyState
, lpChar
, uFlags
, 0);
414 ToAsciiEx(UINT uVirtKey
,
416 CONST BYTE
*lpKeyState
,
424 Ret
= ToUnicodeEx(uVirtKey
, uScanCode
, lpKeyState
, UniChars
, 2, uFlags
, dwhkl
);
425 CharCount
= (Ret
< 0 ? 1 : Ret
);
426 WideCharToMultiByte(CP_ACP
, 0, UniChars
, CharCount
, (LPSTR
) lpChar
, 2, NULL
, NULL
);
436 ToUnicode(UINT wVirtKey
,
438 CONST BYTE
*lpKeyState
,
443 return ToUnicodeEx( wVirtKey
, wScanCode
, lpKeyState
, pwszBuff
, cchBuff
,
452 ToUnicodeEx(UINT wVirtKey
,
454 CONST BYTE
*lpKeyState
,
460 return NtUserToUnicodeEx( wVirtKey
, wScanCode
, (PBYTE
)lpKeyState
, pwszBuff
, cchBuff
,
474 if (IsDBCSLeadByte(ch
)) return -1;
476 MultiByteToWideChar(CP_ACP
, 0, &ch
, 1, &wChar
, 1);
477 return VkKeyScanW(wChar
);
485 VkKeyScanExA(CHAR ch
,
490 if (IsDBCSLeadByte(ch
)) return -1;
492 MultiByteToWideChar(CP_ACP
, 0, &ch
, 1, &wChar
, 1);
493 return VkKeyScanExW(wChar
, dwhkl
);
501 VkKeyScanExW(WCHAR ch
,
504 return (SHORT
) NtUserVkKeyScanEx(ch
, dwhkl
, TRUE
);
514 return (SHORT
) NtUserVkKeyScanEx(ch
, 0, FALSE
);
527 ULONG_PTR dwExtraInfo
)
533 Input
.type
= INPUT_KEYBOARD
;
535 Input
.ki
.wScan
= bScan
;
536 Input
.ki
.dwFlags
= dwFlags
;
538 Input
.ki
.dwExtraInfo
= dwExtraInfo
;
540 NtUserSendInput(1, &Input
, sizeof(INPUT
));
554 ULONG_PTR dwExtraInfo
)
558 Input
.type
= INPUT_MOUSE
;
561 Input
.mi
.mouseData
= dwData
;
562 Input
.mi
.dwFlags
= dwFlags
;
564 Input
.mi
.dwExtraInfo
= dwExtraInfo
;
566 NtUserSendInput(1, &Input
, sizeof(INPUT
));
570 /***********************************************************************
573 static WORD
get_key_state(void)
577 if (GetSystemMetrics( SM_SWAPBUTTON
))
579 if (GetAsyncKeyState(VK_RBUTTON
) & 0x80) ret
|= MK_LBUTTON
;
580 if (GetAsyncKeyState(VK_LBUTTON
) & 0x80) ret
|= MK_RBUTTON
;
584 if (GetAsyncKeyState(VK_LBUTTON
) & 0x80) ret
|= MK_LBUTTON
;
585 if (GetAsyncKeyState(VK_RBUTTON
) & 0x80) ret
|= MK_RBUTTON
;
587 if (GetAsyncKeyState(VK_MBUTTON
) & 0x80) ret
|= MK_MBUTTON
;
588 if (GetAsyncKeyState(VK_SHIFT
) & 0x80) ret
|= MK_SHIFT
;
589 if (GetAsyncKeyState(VK_CONTROL
) & 0x80) ret
|= MK_CONTROL
;
590 if (GetAsyncKeyState(VK_XBUTTON1
) & 0x80) ret
|= MK_XBUTTON1
;
591 if (GetAsyncKeyState(VK_XBUTTON2
) & 0x80) ret
|= MK_XBUTTON2
;
595 static void CALLBACK
TrackMouseEventProc(HWND hwndUnused
, UINT uMsg
, UINT_PTR idEvent
,
601 INT hoverwidth
= 0, hoverheight
= 0;
603 PUSER32_TRACKINGLIST ptracking_info
;
605 ptracking_info
= & User32GetThreadData()->tracking_info
;
608 hwnd
= WindowFromPoint(pos
);
610 SystemParametersInfoW(SPI_GETMOUSEHOVERWIDTH
, 0, &hoverwidth
, 0);
611 SystemParametersInfoW(SPI_GETMOUSEHOVERHEIGHT
, 0, &hoverheight
, 0);
613 /* see if this tracking event is looking for TME_LEAVE and that the */
614 /* mouse has left the window */
615 if (ptracking_info
->tme
.dwFlags
& TME_LEAVE
)
617 if (ptracking_info
->tme
.hwndTrack
!= hwnd
)
619 if (ptracking_info
->tme
.dwFlags
& TME_NONCLIENT
)
621 PostMessageW(ptracking_info
->tme
.hwndTrack
, WM_NCMOUSELEAVE
, 0, 0);
625 PostMessageW(ptracking_info
->tme
.hwndTrack
, WM_MOUSELEAVE
, 0, 0);
628 /* remove the TME_LEAVE flag */
629 ptracking_info
->tme
.dwFlags
&= ~TME_LEAVE
;
633 GetClientRect(hwnd
, &client
);
634 MapWindowPoints(hwnd
, NULL
, (LPPOINT
)&client
, 2);
635 if (PtInRect(&client
, pos
))
637 if (ptracking_info
->tme
.dwFlags
& TME_NONCLIENT
)
639 PostMessageW(ptracking_info
->tme
.hwndTrack
, WM_NCMOUSELEAVE
, 0, 0);
640 /* remove the TME_LEAVE flag */
641 ptracking_info
->tme
.dwFlags
&= ~TME_LEAVE
;
646 if (!(ptracking_info
->tme
.dwFlags
& TME_NONCLIENT
))
648 PostMessageW(ptracking_info
->tme
.hwndTrack
, WM_MOUSELEAVE
, 0, 0);
649 /* remove the TME_LEAVE flag */
650 ptracking_info
->tme
.dwFlags
&= ~TME_LEAVE
;
656 /* see if we are tracking hovering for this hwnd */
657 if (ptracking_info
->tme
.dwFlags
& TME_HOVER
)
659 /* has the cursor moved outside the rectangle centered around pos? */
660 if ((abs(pos
.x
- ptracking_info
->pos
.x
) > (hoverwidth
/ 2.0)) ||
661 (abs(pos
.y
- ptracking_info
->pos
.y
) > (hoverheight
/ 2.0)))
663 /* record this new position as the current position and reset */
664 /* the iHoverTime variable to 0 */
665 ptracking_info
->pos
= pos
;
671 ScreenToClient(hwnd
, &posClient
);
673 if (ptracking_info
->tme
.dwFlags
& TME_NONCLIENT
)
675 PostMessageW(ptracking_info
->tme
.hwndTrack
, WM_NCMOUSEHOVER
,
676 get_key_state(), MAKELPARAM( posClient
.x
, posClient
.y
));
680 PostMessageW(ptracking_info
->tme
.hwndTrack
, WM_MOUSEHOVER
,
681 get_key_state(), MAKELPARAM( posClient
.x
, posClient
.y
));
684 /* stop tracking mouse hover */
685 ptracking_info
->tme
.dwFlags
&= ~TME_HOVER
;
689 /* stop the timer if the tracking list is empty */
690 if (!(ptracking_info
->tme
.dwFlags
& (TME_HOVER
| TME_LEAVE
)))
692 KillTimer(0, ptracking_info
->timer
);
693 RtlZeroMemory(ptracking_info
,sizeof(USER32_TRACKINGLIST
));
698 /***********************************************************************
699 * TrackMouseEvent [USER32]
701 * Requests notification of mouse events
703 * During mouse tracking WM_MOUSEHOVER or WM_MOUSELEAVE events are posted
704 * to the hwnd specified in the ptme structure. After the event message
705 * is posted to the hwnd, the entry in the queue is removed.
707 * If the current hwnd isn't ptme->hwndTrack the TME_HOVER flag is completely
708 * ignored. The TME_LEAVE flag results in a WM_MOUSELEAVE message being posted
709 * immediately and the TME_LEAVE flag being ignored.
712 * ptme [I,O] pointer to TRACKMOUSEEVENT information structure.
725 LPTRACKMOUSEEVENT ptme
)
730 PUSER32_TRACKINGLIST ptracking_info
;
732 TRACE("%lx, %lx, %p, %lx\n", ptme
->cbSize
, ptme
->dwFlags
, ptme
->hwndTrack
, ptme
->dwHoverTime
);
734 if (ptme
->cbSize
!= sizeof(TRACKMOUSEEVENT
)) {
735 WARN("wrong TRACKMOUSEEVENT size from app\n");
736 SetLastError(ERROR_INVALID_PARAMETER
);
740 ptracking_info
= & User32GetThreadData()->tracking_info
;
742 /* fill the TRACKMOUSEEVENT struct with the current tracking for the given hwnd */
743 if (ptme
->dwFlags
& TME_QUERY
)
745 *ptme
= ptracking_info
->tme
;
746 ptme
->cbSize
= sizeof(TRACKMOUSEEVENT
);
748 return TRUE
; /* return here, TME_QUERY is retrieving information */
751 if (!IsWindow(ptme
->hwndTrack
))
753 SetLastError(ERROR_INVALID_WINDOW_HANDLE
);
757 hover_time
= ptme
->dwHoverTime
;
759 /* if HOVER_DEFAULT was specified replace this with the systems current value */
760 if (hover_time
== HOVER_DEFAULT
|| hover_time
== 0)
762 SystemParametersInfoW(SPI_GETMOUSEHOVERTIME
, 0, &hover_time
, 0);
766 hwnd
= WindowFromPoint(pos
);
768 if (ptme
->dwFlags
& ~(TME_CANCEL
| TME_HOVER
| TME_LEAVE
| TME_NONCLIENT
))
770 FIXME("Unknown flag(s) %08lx\n", ptme
->dwFlags
& ~(TME_CANCEL
| TME_HOVER
| TME_LEAVE
| TME_NONCLIENT
));
773 if (ptme
->dwFlags
& TME_CANCEL
)
775 if (ptracking_info
->tme
.hwndTrack
== ptme
->hwndTrack
)
777 ptracking_info
->tme
.dwFlags
&= ~(ptme
->dwFlags
& ~TME_CANCEL
);
779 /* if we aren't tracking on hover or leave remove this entry */
780 if (!(ptracking_info
->tme
.dwFlags
& (TME_HOVER
| TME_LEAVE
)))
782 KillTimer(0, ptracking_info
->timer
);
783 RtlZeroMemory(ptracking_info
,sizeof(USER32_TRACKINGLIST
));
787 if (ptme
->hwndTrack
== hwnd
)
789 /* Adding new mouse event to the tracking list */
790 ptracking_info
->tme
= *ptme
;
791 ptracking_info
->tme
.dwHoverTime
= hover_time
;
793 /* Initialize HoverInfo variables even if not hover tracking */
794 ptracking_info
->pos
= pos
;
796 if (!ptracking_info
->timer
)
798 ptracking_info
->timer
= SetTimer(0, 0, hover_time
, TrackMouseEventProc
);