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
159159 break;
160160
161161 // Only key-down events are interesting to us
162− if (event.EventType != KEY_EVENT)
163− continue;
164− KEY_EVENT_RECORD *record = &event.Event.KeyEvent;
165− if (!record->bKeyDown)
166− continue;
167−
168− UINT vkey = record->wVirtualKeyCode;
169− bool ext = record->dwControlKeyState & ENHANCED_KEY;
170−
171− int mods = 0;
172− if (record->dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED))
173− mods |= MP_KEY_MODIFIER_ALT;
174− if (record->dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))
175− mods |= MP_KEY_MODIFIER_CTRL;
176− if (record->dwControlKeyState & SHIFT_PRESSED)
177− mods |= MP_KEY_MODIFIER_SHIFT;
178−
179− int mpkey = mp_w32_vkey_to_mpkey(vkey, ext);
180− if (mpkey) {
181− mp_input_put_key(input_ctx, mpkey | mods);
182− } else {
183− // Only characters should be remaining
184− int c = record->uChar.UnicodeChar;
185− // The ctrl key always produces control characters in the console.
186− // Shift them back up to regular characters.
187− if (c > 0 && c < 0x20 && (mods & MP_KEY_MODIFIER_CTRL))
188− c += (mods & MP_KEY_MODIFIER_SHIFT) ? 0x40 : 0x60;
189− if (c >= 0x20)
190− mp_input_put_key(input_ctx, c | mods);
162+ switch (event.EventType)
163+ {
164+ case KEY_EVENT: {
165+ KEY_EVENT_RECORD *record = &event.Event.KeyEvent;
166+ if (!record->bKeyDown)
167+ continue;
168+
169+ UINT vkey = record->wVirtualKeyCode;
170+ bool ext = record->dwControlKeyState & ENHANCED_KEY;
171+
172+ int mods = 0;
173+ if (record->dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED))
174+ mods |= MP_KEY_MODIFIER_ALT;
175+ if (record->dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))
176+ mods |= MP_KEY_MODIFIER_CTRL;
177+ if (record->dwControlKeyState & SHIFT_PRESSED)
178+ mods |= MP_KEY_MODIFIER_SHIFT;
179+
180+ int mpkey = mp_w32_vkey_to_mpkey(vkey, ext);
181+ if (mpkey) {
182+ mp_input_put_key(input_ctx, mpkey | mods);
183+ } else {
184+ // Only characters should be remaining
185+ int c = record->uChar.UnicodeChar;
186+ // The ctrl key always produces control characters in the console.
187+ // Shift them back up to regular characters.
188+ if (c > 0 && c < 0x20 && (mods & MP_KEY_MODIFIER_CTRL))
189+ c += (mods & MP_KEY_MODIFIER_SHIFT) ? 0x40 : 0x60;
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+ }
191236 }
192237 }
193238 }

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