History

terminal-win: support mouse input events

The mouse input information is available from the INPUT_RECORD with MOUSE_EVENT event type.

nanahi committed 2 years ago

Commit 7cfd3691 parent 8a2892a1 file · +74 −29Browse files

Unified · Split

osdep/terminal-win.c+74−29
@@ -159,35 +159,80 @@ static void read_input(HANDLE in) · 158 unmodified lines
159 break;159 break;
160 160
161 // Only key-down events are interesting to us161 // Only key-down events are interesting to us
162 if (event.EventType != KEY_EVENT)162 switch (event.EventType)
163 continue;163 {
164 KEY_EVENT_RECORD *record = &event.Event.KeyEvent;164 case KEY_EVENT: {
165 if (!record->bKeyDown)165 KEY_EVENT_RECORD *record = &event.Event.KeyEvent;
166 continue;166 if (!record->bKeyDown)
167 167 continue;
168 UINT vkey = record->wVirtualKeyCode;168
169 bool ext = record->dwControlKeyState & ENHANCED_KEY;169 UINT vkey = record->wVirtualKeyCode;
170 170 bool ext = record->dwControlKeyState & ENHANCED_KEY;
171 int mods = 0;171
172 if (record->dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED))172 int mods = 0;
173 mods |= MP_KEY_MODIFIER_ALT;173 if (record->dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED))
174 if (record->dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))174 mods |= MP_KEY_MODIFIER_ALT;
175 mods |= MP_KEY_MODIFIER_CTRL;175 if (record->dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))
176 if (record->dwControlKeyState & SHIFT_PRESSED)176 mods |= MP_KEY_MODIFIER_CTRL;
177 mods |= MP_KEY_MODIFIER_SHIFT;177 if (record->dwControlKeyState & SHIFT_PRESSED)
178 178 mods |= MP_KEY_MODIFIER_SHIFT;
179 int mpkey = mp_w32_vkey_to_mpkey(vkey, ext);179
180 if (mpkey) {180 int mpkey = mp_w32_vkey_to_mpkey(vkey, ext);
181 mp_input_put_key(input_ctx, mpkey | mods);181 if (mpkey) {
182 } else {182 mp_input_put_key(input_ctx, mpkey | mods);
183 // Only characters should be remaining183 } else {
184 int c = record->uChar.UnicodeChar;184 // Only characters should be remaining
185 // The ctrl key always produces control characters in the console.185 int c = record->uChar.UnicodeChar;
186 // Shift them back up to regular characters.186 // The ctrl key always produces control characters in the console.
187 if (c > 0 && c < 0x20 && (mods & MP_KEY_MODIFIER_CTRL))187 // Shift them back up to regular characters.
188 c += (mods & MP_KEY_MODIFIER_SHIFT) ? 0x40 : 0x60;188 if (c > 0 && c < 0x20 && (mods & MP_KEY_MODIFIER_CTRL))
189 if (c >= 0x20)189 c += (mods & MP_KEY_MODIFIER_SHIFT) ? 0x40 : 0x60;
190 mp_input_put_key(input_ctx, c | mods);190 if (c >= 0x20)
191 mp_input_put_key(input_ctx, c | mods);
192 }
193 break;
194 }
195 case MOUSE_EVENT: {
196 MOUSE_EVENT_RECORD *record = &event.Event.MouseEvent;
197 int mods = 0;
198 if (record->dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED))
199 mods |= MP_KEY_MODIFIER_ALT;
200 if (record->dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))
201 mods |= MP_KEY_MODIFIER_CTRL;
202 if (record->dwControlKeyState & SHIFT_PRESSED)
203 mods |= MP_KEY_MODIFIER_SHIFT;
204
205 switch (record->dwEventFlags) {
206 case MOUSE_MOVED: {
207 int w = 0, h = 0;
208 if (get_font_size(&w, &h)) {
209 mp_input_set_mouse_pos(input_ctx, w * (record->dwMousePosition.X + 0.5),
210 h * (record->dwMousePosition.Y + 0.5));
211 }
212 break;
213 }
214 case MOUSE_HWHEELED: {
215 int button = (int16_t)HIWORD(record->dwButtonState) > 0 ? MP_WHEEL_RIGHT : MP_WHEEL_LEFT;
216 mp_input_put_key(input_ctx, button | mods);
217 break;
218 }
219 case MOUSE_WHEELED: {
220 int button = (int16_t)HIWORD(record->dwButtonState) > 0 ? MP_WHEEL_UP : MP_WHEEL_DOWN;
221 mp_input_put_key(input_ctx, button | mods);
222 break;
223 }
224 default: {
225 int left_button_state = record->dwButtonState & FROM_LEFT_1ST_BUTTON_PRESSED ?
226 MP_KEY_STATE_DOWN : MP_KEY_STATE_UP;
227 mp_input_put_key(input_ctx, MP_MBTN_LEFT | mods | left_button_state);
228 int right_button_state = record->dwButtonState & RIGHTMOST_BUTTON_PRESSED ?
229 MP_KEY_STATE_DOWN : MP_KEY_STATE_UP;
230 mp_input_put_key(input_ctx, MP_MBTN_RIGHT | mods | right_button_state);
231 break;
232 }
233 }
234 break;
235 }
191 }236 }
192 }237 }
193}238}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t