-
-
Notifications
You must be signed in to change notification settings - Fork 219
/
keyboard.go
168 lines (139 loc) · 4.91 KB
/
keyboard.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package sdl
/*
#include "sdl_wrapper.h"
#if !(SDL_VERSION_ATLEAST(2,24,0))
#if defined(WARN_OUTDATED)
#pragma message("SDL_ResetKeyboard is not supported before SDL 2.24.0")
#endif
static inline void SDL_ResetKeyboard(void)
{
return;
}
#endif
#if !(SDL_VERSION_ATLEAST(2,0,22))
#if defined(WARN_OUTDATED)
#pragma message("SDL_IsTextInputShown is not supported before SDL 2.0.22")
#pragma message("SDL_ClearComposition is not supported before SDL 2.0.22")
#endif
static inline SDL_bool SDL_IsTextInputShown(void)
{
return SDL_FALSE;
}
static inline void SDL_ClearComposition(void)
{
}
#endif
*/
import "C"
import (
"reflect"
"unsafe"
)
// Keysym contains key information used in key events.
// (https://wiki.libsdl.org/SDL_Keysym)
type Keysym struct {
Scancode Scancode // SDL physical key code
Sym Keycode // SDL virtual key code
Mod Keymod // current key modifiers
unused uint32 // unused
}
// GetKeyboardFocus returns the window which currently has keyboard focus.
// (https://wiki.libsdl.org/SDL_GetKeyboardFocus)
func GetKeyboardFocus() *Window {
return (*Window)(unsafe.Pointer(C.SDL_GetKeyboardFocus()))
}
// GetKeyboardState returns a snapshot of the current state of the keyboard.
// (https://wiki.libsdl.org/SDL_GetKeyboardState)
func GetKeyboardState() []uint8 {
var numkeys C.int
start := C.SDL_GetKeyboardState(&numkeys)
sh := reflect.SliceHeader{}
sh.Len = int(numkeys)
sh.Cap = int(numkeys)
sh.Data = uintptr(unsafe.Pointer(start))
return *(*[]uint8)(unsafe.Pointer(&sh))
}
// GetModState returns the current key modifier state for the keyboard.
// (https://wiki.libsdl.org/SDL_GetModState)
func GetModState() Keymod {
return (Keymod)(C.SDL_GetModState())
}
// SetModState sets the current key modifier state for the keyboard.
// (https://wiki.libsdl.org/SDL_SetModState)
func SetModState(mod Keymod) {
C.SDL_SetModState(mod.c())
}
// GetKeyFromScancode returns the key code corresponding to the given scancode according to the current keyboard layout.
// (https://wiki.libsdl.org/SDL_GetKeyFromScancode)
func GetKeyFromScancode(code Scancode) Keycode {
return (Keycode)(C.SDL_GetKeyFromScancode(code.c()))
}
// GetScancodeFromKey returns the scancode corresponding to the given key code according to the current keyboard layout.
// (https://wiki.libsdl.org/SDL_GetScancodeFromKey)
func GetScancodeFromKey(code Keycode) Scancode {
return (Scancode)(C.SDL_GetScancodeFromKey(code.c()))
}
// GetScancodeName returns a human-readable name for a scancode
// (https://wiki.libsdl.org/SDL_GetScancodeName)
func GetScancodeName(code Scancode) string {
return (C.GoString)(C.SDL_GetScancodeName(code.c()))
}
// GetScancodeFromName returns a scancode from a human-readable name.
// (https://wiki.libsdl.org/SDL_GetScancodeFromName)
func GetScancodeFromName(name string) Scancode {
_name := C.CString(name)
defer C.free(unsafe.Pointer(_name))
return (Scancode)(C.SDL_GetScancodeFromName(_name))
}
// GetKeyName returns a human-readable name for a key.
// (https://wiki.libsdl.org/SDL_GetKeyName)
func GetKeyName(code Keycode) string {
return (C.GoString)(C.SDL_GetKeyName(code.c()))
}
// GetKeyFromName returns a key code from a human-readable name.
// (https://wiki.libsdl.org/SDL_GetKeyFromName)
func GetKeyFromName(name string) Keycode {
_name := C.CString(name)
defer C.free(unsafe.Pointer(_name))
return (Keycode)(C.SDL_GetKeyFromName(_name))
}
// StartTextInput starts accepting Unicode text input events.
// (https://wiki.libsdl.org/SDL_StartTextInput)
func StartTextInput() {
C.SDL_StartTextInput()
}
// IsTextInputActive checks whether or not Unicode text input events are enabled.
// (https://wiki.libsdl.org/SDL_IsTextInputActive)
func IsTextInputActive() bool {
return C.SDL_IsTextInputActive() > 0
}
// StopTextInput stops receiving any text input events.
// (https://wiki.libsdl.org/SDL_StopTextInput)
func StopTextInput() {
C.SDL_StopTextInput()
}
// SetTextInputRect sets the rectangle used to type Unicode text inputs.
// (https://wiki.libsdl.org/SDL_SetTextInputRect)
func SetTextInputRect(rect *Rect) {
C.SDL_SetTextInputRect(rect.cptr())
}
// HasScreenKeyboardSupport reports whether the platform has some screen keyboard support.
// (https://wiki.libsdl.org/SDL_HasScreenKeyboardSupport)
func HasScreenKeyboardSupport() bool {
return C.SDL_HasScreenKeyboardSupport() > 0
}
// IsScreenKeyboardShown reports whether the screen keyboard is shown for given window.
// (https://wiki.libsdl.org/SDL_IsScreenKeyboardShown)
func IsScreenKeyboardShown(window *Window) bool {
return C.SDL_IsScreenKeyboardShown(window.cptr()) > 0
}
// IsTextInputShown returns if an IME Composite or Candidate window is currently shown.
// (https://wiki.libsdl.org/SDL_IsTextInputShown)
func IsTextInputShown() bool {
return C.SDL_IsTextInputShown() > 0
}
// ClearComposition dismisses the composition window/IME without disabling the subsystem.
// (https://wiki.libsdl.org/SDL_ClearComposition)
func ClearComposition() {
C.SDL_ClearComposition()
}