dear_imgui_rs/
input.rs

1//! Input types (mouse, keyboard, cursors)
2//!
3//! Strongly-typed identifiers for mouse buttons, mouse cursors and keyboard
4//! keys used by Dear ImGui. Backends typically translate platform events into
5//! these enums when feeding input into `Io`.
6//!
7//! See [`io`] for the per-frame input state and configuration.
8//!
9#![allow(
10    clippy::cast_possible_truncation,
11    clippy::cast_sign_loss,
12    clippy::as_conversions
13)]
14use crate::sys;
15use bitflags::bitflags;
16
17/// Mouse button identifier
18#[repr(i32)]
19#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
20pub enum MouseButton {
21    /// Left mouse button
22    Left = sys::ImGuiMouseButton_Left as i32,
23    /// Right mouse button
24    Right = sys::ImGuiMouseButton_Right as i32,
25    /// Middle mouse button
26    Middle = sys::ImGuiMouseButton_Middle as i32,
27    /// Extra mouse button 1 (typically Back)
28    Extra1 = 3,
29    /// Extra mouse button 2 (typically Forward)
30    Extra2 = 4,
31}
32
33/// Mouse cursor types
34#[repr(i32)]
35#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
36pub enum MouseCursor {
37    /// No cursor
38    None = sys::ImGuiMouseCursor_None,
39    /// Arrow cursor
40    Arrow = sys::ImGuiMouseCursor_Arrow,
41    /// Text input I-beam cursor
42    TextInput = sys::ImGuiMouseCursor_TextInput,
43    /// Resize all directions cursor
44    ResizeAll = sys::ImGuiMouseCursor_ResizeAll,
45    /// Resize north-south cursor
46    ResizeNS = sys::ImGuiMouseCursor_ResizeNS,
47    /// Resize east-west cursor
48    ResizeEW = sys::ImGuiMouseCursor_ResizeEW,
49    /// Resize northeast-southwest cursor
50    ResizeNESW = sys::ImGuiMouseCursor_ResizeNESW,
51    /// Resize northwest-southeast cursor
52    ResizeNWSE = sys::ImGuiMouseCursor_ResizeNWSE,
53    /// Hand cursor
54    Hand = sys::ImGuiMouseCursor_Hand,
55    /// Not allowed cursor
56    NotAllowed = sys::ImGuiMouseCursor_NotAllowed,
57}
58
59/// Key identifier for keyboard input
60#[repr(i32)]
61#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
62pub enum Key {
63    /// No key
64    None = sys::ImGuiKey_None as i32,
65    /// Tab key
66    Tab = sys::ImGuiKey_Tab as i32,
67    /// Left arrow key
68    LeftArrow = sys::ImGuiKey_LeftArrow as i32,
69    /// Right arrow key
70    RightArrow = sys::ImGuiKey_RightArrow as i32,
71    /// Up arrow key
72    UpArrow = sys::ImGuiKey_UpArrow as i32,
73    /// Down arrow key
74    DownArrow = sys::ImGuiKey_DownArrow as i32,
75    /// Page up key
76    PageUp = sys::ImGuiKey_PageUp as i32,
77    /// Page down key
78    PageDown = sys::ImGuiKey_PageDown as i32,
79    /// Home key
80    Home = sys::ImGuiKey_Home as i32,
81    /// End key
82    End = sys::ImGuiKey_End as i32,
83    /// Insert key
84    Insert = sys::ImGuiKey_Insert as i32,
85    /// Delete key
86    Delete = sys::ImGuiKey_Delete as i32,
87    /// Backspace key
88    Backspace = sys::ImGuiKey_Backspace as i32,
89    /// Space key
90    Space = sys::ImGuiKey_Space as i32,
91    /// Enter key
92    Enter = sys::ImGuiKey_Enter as i32,
93    /// Escape key
94    Escape = sys::ImGuiKey_Escape as i32,
95    /// Left Ctrl key
96    LeftCtrl = sys::ImGuiKey_LeftCtrl as i32,
97    /// Left Shift key
98    LeftShift = sys::ImGuiKey_LeftShift as i32,
99    /// Left Alt key
100    LeftAlt = sys::ImGuiKey_LeftAlt as i32,
101    /// Left Super key
102    LeftSuper = sys::ImGuiKey_LeftSuper as i32,
103    /// Right Ctrl key
104    RightCtrl = sys::ImGuiKey_RightCtrl as i32,
105    /// Right Shift key
106    RightShift = sys::ImGuiKey_RightShift as i32,
107    /// Right Alt key
108    RightAlt = sys::ImGuiKey_RightAlt as i32,
109    /// Right Super key
110    RightSuper = sys::ImGuiKey_RightSuper as i32,
111    /// Menu key
112    Menu = sys::ImGuiKey_Menu as i32,
113    /// 0 key
114    Key0 = sys::ImGuiKey_0 as i32,
115    /// 1 key
116    Key1 = sys::ImGuiKey_1 as i32,
117    /// 2 key
118    Key2 = sys::ImGuiKey_2 as i32,
119    /// 3 key
120    Key3 = sys::ImGuiKey_3 as i32,
121    /// 4 key
122    Key4 = sys::ImGuiKey_4 as i32,
123    /// 5 key
124    Key5 = sys::ImGuiKey_5 as i32,
125    /// 6 key
126    Key6 = sys::ImGuiKey_6 as i32,
127    /// 7 key
128    Key7 = sys::ImGuiKey_7 as i32,
129    /// 8 key
130    Key8 = sys::ImGuiKey_8 as i32,
131    /// 9 key
132    Key9 = sys::ImGuiKey_9 as i32,
133    /// A key
134    A = sys::ImGuiKey_A as i32,
135    /// B key
136    B = sys::ImGuiKey_B as i32,
137    /// C key
138    C = sys::ImGuiKey_C as i32,
139    /// D key
140    D = sys::ImGuiKey_D as i32,
141    /// E key
142    E = sys::ImGuiKey_E as i32,
143    /// F key
144    F = sys::ImGuiKey_F as i32,
145    /// G key
146    G = sys::ImGuiKey_G as i32,
147    /// H key
148    H = sys::ImGuiKey_H as i32,
149    /// I key
150    I = sys::ImGuiKey_I as i32,
151    /// J key
152    J = sys::ImGuiKey_J as i32,
153    /// K key
154    K = sys::ImGuiKey_K as i32,
155    /// L key
156    L = sys::ImGuiKey_L as i32,
157    /// M key
158    M = sys::ImGuiKey_M as i32,
159    /// N key
160    N = sys::ImGuiKey_N as i32,
161    /// O key
162    O = sys::ImGuiKey_O as i32,
163    /// P key
164    P = sys::ImGuiKey_P as i32,
165    /// Q key
166    Q = sys::ImGuiKey_Q as i32,
167    /// R key
168    R = sys::ImGuiKey_R as i32,
169    /// S key
170    S = sys::ImGuiKey_S as i32,
171    /// T key
172    T = sys::ImGuiKey_T as i32,
173    /// U key
174    U = sys::ImGuiKey_U as i32,
175    /// V key
176    V = sys::ImGuiKey_V as i32,
177    /// W key
178    W = sys::ImGuiKey_W as i32,
179    /// X key
180    X = sys::ImGuiKey_X as i32,
181    /// Y key
182    Y = sys::ImGuiKey_Y as i32,
183    /// Z key
184    Z = sys::ImGuiKey_Z as i32,
185    /// F1 key
186    F1 = sys::ImGuiKey_F1 as i32,
187    /// F2 key
188    F2 = sys::ImGuiKey_F2 as i32,
189    /// F3 key
190    F3 = sys::ImGuiKey_F3 as i32,
191    /// F4 key
192    F4 = sys::ImGuiKey_F4 as i32,
193    /// F5 key
194    F5 = sys::ImGuiKey_F5 as i32,
195    /// F6 key
196    F6 = sys::ImGuiKey_F6 as i32,
197    /// F7 key
198    F7 = sys::ImGuiKey_F7 as i32,
199    /// F8 key
200    F8 = sys::ImGuiKey_F8 as i32,
201    /// F9 key
202    F9 = sys::ImGuiKey_F9 as i32,
203    /// F10 key
204    F10 = sys::ImGuiKey_F10 as i32,
205    /// F11 key
206    F11 = sys::ImGuiKey_F11 as i32,
207    /// F12 key
208    F12 = sys::ImGuiKey_F12 as i32,
209
210    // --- Punctuation and extra named keys ---
211    /// Apostrophe (') key
212    Apostrophe = sys::ImGuiKey_Apostrophe as i32,
213    /// Comma (,) key
214    Comma = sys::ImGuiKey_Comma as i32,
215    /// Minus (-) key
216    Minus = sys::ImGuiKey_Minus as i32,
217    /// Period (.) key
218    Period = sys::ImGuiKey_Period as i32,
219    /// Slash (/) key
220    Slash = sys::ImGuiKey_Slash as i32,
221    /// Semicolon (;) key
222    Semicolon = sys::ImGuiKey_Semicolon as i32,
223    /// Equal (=) key
224    Equal = sys::ImGuiKey_Equal as i32,
225    /// Left bracket ([) key
226    LeftBracket = sys::ImGuiKey_LeftBracket as i32,
227    /// Backslash (\) key
228    Backslash = sys::ImGuiKey_Backslash as i32,
229    /// Right bracket (]) key
230    RightBracket = sys::ImGuiKey_RightBracket as i32,
231    /// Grave accent (`) key
232    GraveAccent = sys::ImGuiKey_GraveAccent as i32,
233    /// CapsLock key
234    CapsLock = sys::ImGuiKey_CapsLock as i32,
235    /// ScrollLock key
236    ScrollLock = sys::ImGuiKey_ScrollLock as i32,
237    /// NumLock key
238    NumLock = sys::ImGuiKey_NumLock as i32,
239    /// PrintScreen key
240    PrintScreen = sys::ImGuiKey_PrintScreen as i32,
241    /// Pause key
242    Pause = sys::ImGuiKey_Pause as i32,
243
244    // --- Keypad ---
245    /// Numpad 0
246    Keypad0 = sys::ImGuiKey_Keypad0 as i32,
247    /// Numpad 1
248    Keypad1 = sys::ImGuiKey_Keypad1 as i32,
249    /// Numpad 2
250    Keypad2 = sys::ImGuiKey_Keypad2 as i32,
251    /// Numpad 3
252    Keypad3 = sys::ImGuiKey_Keypad3 as i32,
253    /// Numpad 4
254    Keypad4 = sys::ImGuiKey_Keypad4 as i32,
255    /// Numpad 5
256    Keypad5 = sys::ImGuiKey_Keypad5 as i32,
257    /// Numpad 6
258    Keypad6 = sys::ImGuiKey_Keypad6 as i32,
259    /// Numpad 7
260    Keypad7 = sys::ImGuiKey_Keypad7 as i32,
261    /// Numpad 8
262    Keypad8 = sys::ImGuiKey_Keypad8 as i32,
263    /// Numpad 9
264    Keypad9 = sys::ImGuiKey_Keypad9 as i32,
265    /// Numpad decimal
266    KeypadDecimal = sys::ImGuiKey_KeypadDecimal as i32,
267    /// Numpad divide
268    KeypadDivide = sys::ImGuiKey_KeypadDivide as i32,
269    /// Numpad multiply
270    KeypadMultiply = sys::ImGuiKey_KeypadMultiply as i32,
271    /// Numpad subtract
272    KeypadSubtract = sys::ImGuiKey_KeypadSubtract as i32,
273    /// Numpad add
274    KeypadAdd = sys::ImGuiKey_KeypadAdd as i32,
275    /// Numpad enter
276    KeypadEnter = sys::ImGuiKey_KeypadEnter as i32,
277    /// Numpad equal
278    KeypadEqual = sys::ImGuiKey_KeypadEqual as i32,
279
280    /// OEM 102 key (ISO < > |)
281    Oem102 = sys::ImGuiKey_Oem102 as i32,
282}
283
284impl From<MouseButton> for sys::ImGuiMouseButton {
285    #[inline]
286    fn from(value: MouseButton) -> sys::ImGuiMouseButton {
287        value as sys::ImGuiMouseButton
288    }
289}
290
291impl From<Key> for sys::ImGuiKey {
292    #[inline]
293    fn from(value: Key) -> sys::ImGuiKey {
294        value as sys::ImGuiKey
295    }
296}
297
298// Key modifier flags are available via io.KeyCtrl/KeyShift/KeyAlt/KeySuper.
299
300bitflags! {
301    /// Input text flags for text input widgets
302    #[repr(transparent)]
303    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
304    pub struct InputTextFlags: i32 {
305        /// No flags
306        const NONE = sys::ImGuiInputTextFlags_None as i32;
307        /// Allow 0123456789.+-*/
308        const CHARS_DECIMAL = sys::ImGuiInputTextFlags_CharsDecimal as i32;
309        /// Allow 0123456789ABCDEFabcdef
310        const CHARS_HEXADECIMAL = sys::ImGuiInputTextFlags_CharsHexadecimal as i32;
311        /// Turn a..z into A..Z
312        const CHARS_UPPERCASE = sys::ImGuiInputTextFlags_CharsUppercase as i32;
313        /// Filter out spaces, tabs
314        const CHARS_NO_BLANK = sys::ImGuiInputTextFlags_CharsNoBlank as i32;
315        /// Select entire text when first taking mouse focus
316        const AUTO_SELECT_ALL = sys::ImGuiInputTextFlags_AutoSelectAll as i32;
317        /// Return 'true' when Enter is pressed (as opposed to every time the value was modified)
318        const ENTER_RETURNS_TRUE = sys::ImGuiInputTextFlags_EnterReturnsTrue as i32;
319        /// Callback on pressing TAB (for completion handling)
320        const CALLBACK_COMPLETION = sys::ImGuiInputTextFlags_CallbackCompletion as i32;
321        /// Callback on pressing Up/Down arrows (for history handling)
322        const CALLBACK_HISTORY = sys::ImGuiInputTextFlags_CallbackHistory as i32;
323        /// Callback on each iteration (user can query cursor and modify text)
324        const CALLBACK_ALWAYS = sys::ImGuiInputTextFlags_CallbackAlways as i32;
325        /// Callback on character inputs to replace or discard them
326        const CALLBACK_CHAR_FILTER = sys::ImGuiInputTextFlags_CallbackCharFilter as i32;
327        /// Pressing TAB input a '\t' character into the text field
328        const ALLOW_TAB_INPUT = sys::ImGuiInputTextFlags_AllowTabInput as i32;
329        /// In multi-line mode, unfocus with Enter, add new line with Ctrl+Enter
330        const CTRL_ENTER_FOR_NEW_LINE = sys::ImGuiInputTextFlags_CtrlEnterForNewLine as i32;
331        /// Disable following the cursor horizontally
332        const NO_HORIZONTAL_SCROLL = sys::ImGuiInputTextFlags_NoHorizontalScroll as i32;
333        /// Overwrite mode
334        const ALWAYS_OVERWRITE = sys::ImGuiInputTextFlags_AlwaysOverwrite as i32;
335        /// Read-only mode
336        const READ_ONLY = sys::ImGuiInputTextFlags_ReadOnly as i32;
337        /// Password mode, display all characters as '*'
338        const PASSWORD = sys::ImGuiInputTextFlags_Password as i32;
339        /// Disable undo/redo
340        const NO_UNDO_REDO = sys::ImGuiInputTextFlags_NoUndoRedo as i32;
341        /// Allow 0123456789.+-*/eE (Scientific notation input)
342        const CHARS_SCIENTIFIC = sys::ImGuiInputTextFlags_CharsScientific as i32;
343        /// Callback on buffer capacity changes request
344        const CALLBACK_RESIZE = sys::ImGuiInputTextFlags_CallbackResize as i32;
345        /// Callback on any edit (note that InputText() already returns true on edit)
346        const CALLBACK_EDIT = sys::ImGuiInputTextFlags_CallbackEdit as i32;
347    }
348}
349
350// TODO: Add NavInput enum once we have proper constants in sys crate
351
352impl crate::Ui {
353    /// Check if a key is being held down
354    #[doc(alias = "IsKeyDown")]
355    pub fn is_key_down(&self, key: Key) -> bool {
356        unsafe { sys::igIsKeyDown_Nil(key as sys::ImGuiKey) }
357    }
358
359    /// Check if a key was pressed (went from !Down to Down)
360    #[doc(alias = "IsKeyPressed")]
361    pub fn is_key_pressed(&self, key: Key) -> bool {
362        unsafe { sys::igIsKeyPressed_Bool(key as sys::ImGuiKey, true) }
363    }
364
365    /// Check if a key was pressed (went from !Down to Down), with repeat
366    #[doc(alias = "IsKeyPressed")]
367    pub fn is_key_pressed_with_repeat(&self, key: Key, repeat: bool) -> bool {
368        unsafe { sys::igIsKeyPressed_Bool(key as sys::ImGuiKey, repeat) }
369    }
370
371    /// Check if a key was released (went from Down to !Down)
372    #[doc(alias = "IsKeyReleased")]
373    pub fn is_key_released(&self, key: Key) -> bool {
374        unsafe { sys::igIsKeyReleased_Nil(key as sys::ImGuiKey) }
375    }
376
377    /// Check if a mouse button is being held down
378    #[doc(alias = "IsMouseDown")]
379    pub fn is_mouse_down(&self, button: MouseButton) -> bool {
380        unsafe { sys::igIsMouseDown_Nil(button.into()) }
381    }
382
383    /// Check if a mouse button was clicked (went from !Down to Down)
384    #[doc(alias = "IsMouseClicked")]
385    pub fn is_mouse_clicked(&self, button: MouseButton) -> bool {
386        unsafe { sys::igIsMouseClicked_Bool(button.into(), false) }
387    }
388
389    /// Check if a mouse button was clicked, with repeat
390    #[doc(alias = "IsMouseClicked")]
391    pub fn is_mouse_clicked_with_repeat(&self, button: MouseButton, repeat: bool) -> bool {
392        unsafe { sys::igIsMouseClicked_Bool(button.into(), repeat) }
393    }
394
395    /// Check if a mouse button was released (went from Down to !Down)
396    #[doc(alias = "IsMouseReleased")]
397    pub fn is_mouse_released(&self, button: MouseButton) -> bool {
398        unsafe { sys::igIsMouseReleased_Nil(button.into()) }
399    }
400
401    /// Check if a mouse button was double-clicked
402    #[doc(alias = "IsMouseDoubleClicked")]
403    pub fn is_mouse_double_clicked(&self, button: MouseButton) -> bool {
404        unsafe { sys::igIsMouseDoubleClicked_Nil(button.into()) }
405    }
406
407    /// Get mouse position in screen coordinates
408    #[doc(alias = "GetMousePos")]
409    pub fn mouse_pos(&self) -> [f32; 2] {
410        let pos = unsafe { sys::igGetMousePos() };
411        [pos.x, pos.y]
412    }
413
414    /// Get mouse position when a specific button was clicked
415    #[doc(alias = "GetMousePosOnOpeningCurrentPopup")]
416    pub fn mouse_pos_on_opening_current_popup(&self) -> [f32; 2] {
417        let pos = unsafe { sys::igGetMousePosOnOpeningCurrentPopup() };
418        [pos.x, pos.y]
419    }
420
421    /// Check if mouse is hovering given rectangle
422    #[doc(alias = "IsMouseHoveringRect")]
423    pub fn is_mouse_hovering_rect(&self, r_min: [f32; 2], r_max: [f32; 2]) -> bool {
424        unsafe {
425            sys::igIsMouseHoveringRect(
426                sys::ImVec2::new(r_min[0], r_min[1]),
427                sys::ImVec2::new(r_max[0], r_max[1]),
428                true,
429            )
430        }
431    }
432
433    /// Check if mouse is hovering given rectangle (with clipping test)
434    #[doc(alias = "IsMouseHoveringRect")]
435    pub fn is_mouse_hovering_rect_with_clip(
436        &self,
437        r_min: [f32; 2],
438        r_max: [f32; 2],
439        clip: bool,
440    ) -> bool {
441        unsafe {
442            sys::igIsMouseHoveringRect(
443                sys::ImVec2::new(r_min[0], r_min[1]),
444                sys::ImVec2::new(r_max[0], r_max[1]),
445                clip,
446            )
447        }
448    }
449
450    /// Check if mouse is dragging
451    #[doc(alias = "IsMouseDragging")]
452    pub fn is_mouse_dragging(&self, button: MouseButton) -> bool {
453        unsafe { sys::igIsMouseDragging(button as i32, -1.0) }
454    }
455
456    /// Check if mouse is dragging with threshold
457    #[doc(alias = "IsMouseDragging")]
458    pub fn is_mouse_dragging_with_threshold(
459        &self,
460        button: MouseButton,
461        lock_threshold: f32,
462    ) -> bool {
463        unsafe { sys::igIsMouseDragging(button as i32, lock_threshold) }
464    }
465
466    /// Get mouse drag delta
467    #[doc(alias = "GetMouseDragDelta")]
468    pub fn mouse_drag_delta(&self, button: MouseButton) -> [f32; 2] {
469        let delta = unsafe { sys::igGetMouseDragDelta(button as i32, -1.0) };
470        [delta.x, delta.y]
471    }
472
473    /// Get mouse drag delta with threshold
474    #[doc(alias = "GetMouseDragDelta")]
475    pub fn mouse_drag_delta_with_threshold(
476        &self,
477        button: MouseButton,
478        lock_threshold: f32,
479    ) -> [f32; 2] {
480        let delta = unsafe { sys::igGetMouseDragDelta(button as i32, lock_threshold) };
481        [delta.x, delta.y]
482    }
483
484    /// Reset mouse drag delta for a specific button
485    #[doc(alias = "ResetMouseDragDelta")]
486    pub fn reset_mouse_drag_delta(&self, button: MouseButton) {
487        unsafe { sys::igResetMouseDragDelta(button as i32) }
488    }
489}