1 /******************************************************************************
2 *
3 * Copyright (C) 2004-2008, The Gentee Group. All rights reserved.
4 * This file is part of the Gentee open source project - http://www.gentee.com.
5 *
6 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE GENTEE LICENSE ("AGREEMENT").
7 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE CONSTITUTES RECIPIENTS
8 * ACCEPTANCE OF THE AGREEMENT.
9 *
10 * Author: Alexey Krivonogov ( gentee )
11 *
12 ******************************************************************************/
13
14 /*-----------------------------------------------------------------------------
15 * Id: keyboard L "Keyboard"
16 *
17 * Summary: These functions are used to emulate the work of the keyboard.
18 For using this library, it is
19 required to specify the file keyboard.g (from lib\keyboard
20 subfolder) with include command. #srcg[
21 |include : $"...\gentee\lib\keyboard\keyboard.g"]
22 *
23 * List: *,sendstr,sendvkey
24 *
25 -----------------------------------------------------------------------------*/
26
27 define <export>
28 {
29 INPUT_MOUSE = 0
30 INPUT_KEYBOARD = 1
31 INPUT_HARDWARE = 2
32 /*-----------------------------------------------------------------------------
33 * Id: keycontrols D
34 *
35 * Summary: Key controls.
36 *
37 -----------------------------------------------------------------------------*/
38 SVK_SHIFT = 0x0001 // #b(Shift) is pressed.
39 SVK_ALT = 0x0002 // #b(Alt) is pressed.
40 SVK_CONTROL = 0x0004 // #b(Ctrl) is pressed.
41
42 //-----------------------------------------------------------------------------
43
44 KEYEVENTF_EXTENDEDKEY = 0x0001
45 KEYEVENTF_KEYUP = 0x0002
46 KEYEVENTF_UNICODE = 0x0004
47 KEYEVENTF_SCANCODE = 0x0008
48
49 VK_BACK = 0x08
50 VK_TAB = 0x09
51
52 VK_CLEAR = 0x0C
53 VK_RETURN = 0x0D
54
55 VK_SHIFT = 0x10
56 VK_CONTROL = 0x11
57 VK_MENU = 0x12
58 VK_PAUSE = 0x13
59 VK_CAPITAL = 0x14
60 VK_ESCAPE = 0x1B
61
62 VK_SPACE = 0x20
63 VK_PRIOR = 0x21
64 VK_NEXT = 0x22
65 VK_END = 0x23
66 VK_HOME = 0x24
67 VK_LEFT = 0x25
68 VK_UP = 0x26
69 VK_RIGHT = 0x27
70 VK_DOWN = 0x28
71 VK_SELECT = 0x29
72 VK_PRINT = 0x2A
73 VK_EXECUTE = 0x2B
74 VK_SNAPSHOT = 0x2C
75 VK_INSERT = 0x2D
76 VK_DELETE = 0x2E
77
78 VK_F1 = 0x70
79 VK_F2 = 0x71
80 VK_F3 = 0x72
81 VK_F4 = 0x73
82 VK_F5 = 0x74
83 VK_F6 = 0x75
84 VK_F7 = 0x76
85 VK_F8 = 0x77
86 VK_F9 = 0x78
87 VK_F10 = 0x79
88 VK_F11 = 0x7A
89 VK_F12 = 0x7B
90 }
91
92 type MOUSEINPUT
93 {
94 int dx
95 int dy
96 uint mouseData
97 uint dwFlags
98 uint time
99 uint dwExtraInfo
100 }
101
102 type KEYBDINPUT
103 {
104 ushort wVk
105 ushort wScan
106 uint dwFlags
107 uint time
108 uint dwExtraInfo
109 }
110
111 type HARDWAREINPUT
112 {
113 uint uMsg
114 ushort wParamL
115 ushort wParamH
116 }
117
118 type INPUT {
119 uint typei
120 MOUSEINPUT mi
121 }
122
123 import "user32.dll" {
124 uint GetKeyState( uint )
125 uint OemKeyScan( ushort )
126 uint SendInput( uint, uint, uint )
127 ushort VkKeyScanA( uint ) -> VkKeyScan
128 }
129
130 func newkey( arr ain of INPUT, ushort vk, uint flag )
131 {
132 uint i
133
134 i = ain.expand( 1 )
135
136 ain[ i ].typei = $INPUT_KEYBOARD
137 ain[ i ].mi->KEYBDINPUT.wVk = vk
138 ain[ i ].mi->KEYBDINPUT.dwFlags = flag
139 ain[ i ].mi->KEYBDINPUT.time = 200
140 }
141
142 /*-----------------------------------------------------------------------------
143 * Id: sendvkey F
144 *
145 * Summary: Pressing a key. Press a key alone or together with
146 #b('Shift, Ctrl, Alt').
147 *
148 * Params: vkey - Virtual key code.
149 flag - Flags for pressing additional keys.$$[keycontrols]
150 *
151 * Return: #lng/retf#
152 *
153 -----------------------------------------------------------------------------*/
154
155 func uint sendvkey( ushort vkey, uint flag )
156 {
157 arr ain of INPUT
158
159 if flag & $SVK_SHIFT : newkey( ain, $VK_SHIFT, 0 )
160 if flag & $SVK_ALT : newkey( ain, $VK_MENU, 0 )
161 if flag & $SVK_CONTROL : newkey( ain, $VK_CONTROL, 0 )
162
163 newkey( ain, vkey, 0 )
164 newkey( ain, vkey, $KEYEVENTF_KEYUP )
165
166 if flag & $SVK_SHIFT : newkey( ain, $VK_SHIFT, $KEYEVENTF_KEYUP )
167 if flag & $SVK_ALT : newkey( ain, $VK_MENU, $KEYEVENTF_KEYUP )
168 if flag & $SVK_CONTROL : newkey( ain, $VK_CONTROL, $KEYEVENTF_KEYUP )
169
170 return SendInput( *ain, ain.ptr(), sizeof( INPUT ))
171 }
172
173 /*-----------------------------------------------------------------------------
174 ** Id: sendstr F
175 *
176 * Summary: Types a string on the keyboard.
177 *
178 * Params: data - The string to be typed on the keyboard.
179 *
180 * Return: #lng/retf#
181 *
182 -----------------------------------------------------------------------------*/
183
184 func uint sendstr( str input )
185 {
186 uint i cur shift code capslock
187 arr ain of INPUT
188
189 capslock = GetKeyState( $VK_CAPITAL ) & 0x01
190 fornum i, *input
191 {
192 // if input[i] == 0x0D : continue
193
194 cur = VkKeyScan( input[i] )
195 shift = ( cur >> 8 ) & 0x01
196 code = cur & 0xFF
197 if capslock && code >= 'A' && code <= 'Z' : shift = !shift
198
199 if shift : newkey( ain, $VK_SHIFT, 0 )
200
201 newkey( ain, code, 0 )
202 newkey( ain, code, $KEYEVENTF_KEYUP )
203
204 if shift : newkey( ain, $VK_SHIFT, $KEYEVENTF_KEYUP )
205 }
206 return SendInput( *ain, ain.ptr(), sizeof( INPUT ))
207 }
208