input.go (7107B)
1 // Copyright 2015 Hajime Hoshi 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package ebiten 16 17 import ( 18 "github.com/hajimehoshi/ebiten/v2/internal/driver" 19 ) 20 21 // InputChars return "printable" runes read from the keyboard at the time update is called. 22 // 23 // InputChars represents the environment's locale-dependent translation of keyboard 24 // input to Unicode characters. 25 // 26 // IsKeyPressed is based on a mapping of device (US keyboard) codes to input device keys. 27 // "Control" and modifier keys should be handled with IsKeyPressed. 28 // 29 // InputChars is concurrent-safe. 30 // 31 // On Android (ebitenmobile), EbitenView must be focusable to enable to handle keyboard keys. 32 // 33 // Keyboards don't work on iOS yet (#1090). 34 func InputChars() []rune { 35 rb := uiDriver().Input().RuneBuffer() 36 return append(make([]rune, 0, len(rb)), rb...) 37 } 38 39 // IsKeyPressed returns a boolean indicating whether key is pressed. 40 // 41 // If you want to know whether the key started being pressed in the current frame, 42 // use inpututil.IsKeyJustPressed 43 // 44 // Known issue: On Edge browser, some keys don't work well: 45 // 46 // - KeyKPEnter and KeyKPEqual are recognized as KeyEnter and KeyEqual. 47 // - KeyPrintScreen is only treated at keyup event. 48 // 49 // IsKeyPressed is concurrent-safe. 50 // 51 // On Android (ebitenmobile), EbitenView must be focusable to enable to handle keyboard keys. 52 // 53 // Keyboards don't work on iOS yet (#1090). 54 func IsKeyPressed(key Key) bool { 55 // There are keys that are invalid values as ebiten.Key (e.g., driver.KeyLeftAlt). 56 // Skip such values. 57 if !key.isValid() { 58 return false 59 } 60 61 var keys []driver.Key 62 switch key { 63 case KeyAlt: 64 keys = []driver.Key{driver.KeyLeftAlt, driver.KeyRightAlt} 65 case KeyControl: 66 keys = []driver.Key{driver.KeyLeftControl, driver.KeyRightControl} 67 case KeyShift: 68 keys = []driver.Key{driver.KeyLeftShift, driver.KeyRightShift} 69 default: 70 keys = []driver.Key{driver.Key(key)} 71 } 72 for _, k := range keys { 73 if uiDriver().Input().IsKeyPressed(k) { 74 return true 75 } 76 } 77 return false 78 } 79 80 // CursorPosition returns a position of a mouse cursor relative to the game screen (window). The cursor position is 81 // 'logical' position and this considers the scale of the screen. 82 // 83 // CursorPosition is concurrent-safe. 84 func CursorPosition() (x, y int) { 85 return uiDriver().Input().CursorPosition() 86 } 87 88 // Wheel returns the x and y offset of the mouse wheel or touchpad scroll. 89 // It returns 0 if the wheel isn't being rolled. 90 // 91 // Wheel is concurrent-safe. 92 func Wheel() (xoff, yoff float64) { 93 return uiDriver().Input().Wheel() 94 } 95 96 // IsMouseButtonPressed returns a boolean indicating whether mouseButton is pressed. 97 // 98 // If you want to know whether the mouseButton started being pressed in the current frame, 99 // use inpututil.IsMouseButtonJustPressed 100 // 101 // IsMouseButtonPressed is concurrent-safe. 102 func IsMouseButtonPressed(mouseButton MouseButton) bool { 103 return uiDriver().Input().IsMouseButtonPressed(driver.MouseButton(mouseButton)) 104 } 105 106 // GamepadID represents a gamepad's identifier. 107 type GamepadID = driver.GamepadID 108 109 // GamepadSDLID returns a string with the GUID generated in the same way as SDL. 110 // To detect devices, see also the community project of gamepad devices database: https://github.com/gabomdq/SDL_GameControllerDB 111 // 112 // GamepadSDLID always returns an empty string on browsers and mobiles. 113 // 114 // GamepadSDLID is concurrent-safe. 115 func GamepadSDLID(id GamepadID) string { 116 return uiDriver().Input().GamepadSDLID(id) 117 } 118 119 // GamepadName returns a string with the name. 120 // This function may vary in how it returns descriptions for the same device across platforms. 121 // for example the following drivers/platforms see a Xbox One controller as the following: 122 // 123 // - Windows: "Xbox Controller" 124 // - Chrome: "Xbox 360 Controller (XInput STANDARD GAMEPAD)" 125 // - Firefox: "xinput" 126 // 127 // GamepadName always returns an empty string on mobiles. 128 // 129 // GamepadName is concurrent-safe. 130 func GamepadName(id GamepadID) string { 131 return uiDriver().Input().GamepadName(id) 132 } 133 134 // GamepadIDs returns a slice indicating available gamepad IDs. 135 // 136 // GamepadIDs is concurrent-safe. 137 // 138 // GamepadIDs always returns an empty slice on mobiles. 139 func GamepadIDs() []GamepadID { 140 return uiDriver().Input().GamepadIDs() 141 } 142 143 // GamepadAxisNum returns the number of axes of the gamepad (id). 144 // 145 // GamepadAxisNum is concurrent-safe. 146 // 147 // GamepadAxisNum always returns 0 on mobiles. 148 func GamepadAxisNum(id GamepadID) int { 149 return uiDriver().Input().GamepadAxisNum(id) 150 } 151 152 // GamepadAxis returns the float value [-1.0 - 1.0] of the given gamepad (id)'s axis (axis). 153 // 154 // GamepadAxis is concurrent-safe. 155 // 156 // GamepadAxis always returns 0 on mobiles. 157 func GamepadAxis(id GamepadID, axis int) float64 { 158 return uiDriver().Input().GamepadAxis(id, axis) 159 } 160 161 // GamepadButtonNum returns the number of the buttons of the given gamepad (id). 162 // 163 // GamepadButtonNum is concurrent-safe. 164 // 165 // GamepadButtonNum always returns 0 on mobiles. 166 func GamepadButtonNum(id GamepadID) int { 167 return uiDriver().Input().GamepadButtonNum(id) 168 } 169 170 // IsGamepadButtonPressed returns the boolean indicating the given button of the gamepad (id) is pressed or not. 171 // 172 // If you want to know whether the given button of gamepad (id) started being pressed in the current frame, 173 // use inpututil.IsGamepadButtonJustPressed 174 // 175 // IsGamepadButtonPressed is concurrent-safe. 176 // 177 // The relationships between physical buttons and buttion IDs depend on environments. 178 // There can be differences even between Chrome and Firefox. 179 // 180 // IsGamepadButtonPressed always returns false on mobiles. 181 func IsGamepadButtonPressed(id GamepadID, button GamepadButton) bool { 182 return uiDriver().Input().IsGamepadButtonPressed(id, driver.GamepadButton(button)) 183 } 184 185 // TouchID represents a touch's identifier. 186 type TouchID = driver.TouchID 187 188 // TouchIDs returns the current touch states. 189 // 190 // If you want to know whether a touch started being pressed in the current frame, 191 // use inpututil.JustPressedTouchIDs 192 // 193 // TouchIDs returns nil when there are no touches. 194 // TouchIDs always returns nil on desktops. 195 // 196 // TouchIDs is concurrent-safe. 197 func TouchIDs() []TouchID { 198 return uiDriver().Input().TouchIDs() 199 } 200 201 // TouchPosition returns the position for the touch of the specified ID. 202 // 203 // If the touch of the specified ID is not present, TouchPosition returns (0, 0). 204 // 205 // TouchPosition is cuncurrent-safe. 206 func TouchPosition(id TouchID) (int, int) { 207 found := false 208 for _, i := range uiDriver().Input().TouchIDs() { 209 if id == i { 210 found = true 211 break 212 } 213 } 214 if !found { 215 return 0, 0 216 } 217 218 return uiDriver().Input().TouchPosition(id) 219 }