1 //========================================================================
2 // GLFW - An OpenGL framework
3 // Platform: Win32/WGL
4 // API version: 2.7
5 // WWW: http://www.glfw.org/
6 //------------------------------------------------------------------------
7 // Copyright (c) 2002-2006 Marcus Geelnard
8 // Copyright (c) 2006-2010 Camilla Berglund <elmindreda@elmindreda.org>
9 //
10 // This software is provided 'as-is', without any express or implied
11 // warranty. In no event will the authors be held liable for any damages
12 // arising from the use of this software.
13 //
14 // Permission is granted to anyone to use this software for any purpose,
15 // including commercial applications, and to alter it and redistribute it
16 // freely, subject to the following restrictions:
17 //
18 // 1. The origin of this software must not be misrepresented; you must not
19 // claim that you wrote the original software. If you use this software
20 // in a product, an acknowledgment in the product documentation would
21 // be appreciated but is not required.
22 //
23 // 2. Altered source versions must be plainly marked as such, and must not
24 // be misrepresented as being the original software.
25 //
26 // 3. This notice may not be removed or altered from any source
27 // distribution.
28 //
29 //========================================================================
30
31 #include "internal.h"
32
33
34 //************************************************************************
35 //**** GLFW internal functions ****
36 //************************************************************************
37
38 //========================================================================
39 // Low level keyboard hook (system callback) function
40 // Used to disable system keys under Windows NT
41 //========================================================================
42
keyboardHook(int nCode,WPARAM wParam,LPARAM lParam)43 static LRESULT CALLBACK keyboardHook( int nCode, WPARAM wParam, LPARAM lParam )
44 {
45 BOOL syskeys = FALSE;
46 PKBDLLHOOKSTRUCT p;
47
48 // We are only looking for keyboard events - interpret lParam as a
49 // pointer to a KBDLLHOOKSTRUCT
50 p = (PKBDLLHOOKSTRUCT) lParam;
51
52 if( nCode == HC_ACTION )
53 {
54 // We have a keyboard event
55
56 switch( wParam )
57 {
58 case WM_KEYDOWN:
59 case WM_SYSKEYDOWN:
60 case WM_KEYUP:
61 case WM_SYSKEYUP:
62 // Detect: ALT+TAB, ALT+ESC, ALT+F4, CTRL+ESC,
63 // LWIN, RWIN, APPS (mysterious menu key)
64 syskeys = ( p->vkCode == VK_TAB &&
65 p->flags & LLKHF_ALTDOWN ) ||
66 ( p->vkCode == VK_ESCAPE &&
67 p->flags & LLKHF_ALTDOWN ) ||
68 ( p->vkCode == VK_F4 &&
69 p->flags & LLKHF_ALTDOWN ) ||
70 ( p->vkCode == VK_ESCAPE &&
71 (GetKeyState(VK_CONTROL) & 0x8000)) ||
72 p->vkCode == VK_LWIN ||
73 p->vkCode == VK_RWIN ||
74 p->vkCode == VK_APPS;
75 break;
76
77 default:
78 break;
79 }
80 }
81
82 // Was it a system key combination (e.g. ALT+TAB)?
83 if( syskeys )
84 {
85 // Pass the key event to our window message loop
86 if( _glfwWin.opened )
87 {
88 PostMessage( _glfwWin.window, (UINT) wParam, p->vkCode, 0 );
89 }
90
91 // We've taken care of it - don't let the system know about this
92 // key event
93 return 1;
94 }
95 else
96 {
97 // It's a harmless key press, let the system deal with it
98 return CallNextHookEx( _glfwWin.keyboardHook, nCode, wParam, lParam );
99 }
100 }
101
102
103
104 //************************************************************************
105 //**** Platform implementation functions ****
106 //************************************************************************
107
108 //========================================================================
109 // Enable system keys
110 //========================================================================
111
_glfwPlatformEnableSystemKeys(void)112 void _glfwPlatformEnableSystemKeys( void )
113 {
114 BOOL dummy;
115
116 // Use different methods depending on operating system version
117 if( _glfwLibrary.Sys.winVer >= _GLFW_WIN_NT4 )
118 {
119 if( _glfwWin.keyboardHook != NULL )
120 {
121 UnhookWindowsHookEx( _glfwWin.keyboardHook );
122 _glfwWin.keyboardHook = NULL;
123 }
124 }
125 else
126 {
127 (void) SystemParametersInfo( SPI_SETSCREENSAVERRUNNING, FALSE, &dummy, 0 );
128 }
129 }
130
131 //========================================================================
132 // Disable system keys
133 //========================================================================
134
_glfwPlatformDisableSystemKeys(void)135 void _glfwPlatformDisableSystemKeys( void )
136 {
137 BOOL dummy;
138
139 // Use different methods depending on operating system version
140 if( _glfwLibrary.Sys.winVer >= _GLFW_WIN_NT4 )
141 {
142 // Under Windows NT, install a low level keyboard hook
143 _glfwWin.keyboardHook = SetWindowsHookEx( WH_KEYBOARD_LL,
144 keyboardHook,
145 _glfwLibrary.instance,
146 0 );
147 }
148 else
149 {
150 // Under Windows 95/98/ME, fool Windows that a screensaver
151 // is running => prevents ALT+TAB, CTRL+ESC and CTRL+ALT+DEL
152 (void) SystemParametersInfo( SPI_SETSCREENSAVERRUNNING, TRUE, &dummy, 0 );
153 }
154 }
155
156