input_examples.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. .. _doc_input_examples:
  2. Input examples
  3. ==============
  4. Introduction
  5. ------------
  6. In this tutorial, you'll learn how to use Godot's :ref:`InputEvent <class_InputEvent>`
  7. system to capture player input. There are many different types of input your
  8. game may use - keyboard, gamepad, mouse, etc. - and many different ways to
  9. turn those inputs into actions in your game. This document will show you some
  10. of the most common scenarios, which you can use as starting points for your
  11. own projects.
  12. .. note:: For a detailed overview of how Godot's input event system works,
  13. see :ref:`doc_inputevent`.
  14. Events versus polling
  15. ---------------------
  16. Sometimes you want your game to respond to a certain input event - pressing
  17. the "jump" button, for example. For other situations, you might want something
  18. to happen as long as a key is pressed, such as movement. In the first case,
  19. you can use the ``_input()`` function, which will be called whenever an input
  20. event occurs. In the second case, Godot provides the :ref:`Input <class_Input>`
  21. singleton, which you can use to query the state of an input.
  22. Examples:
  23. .. tabs::
  24. .. code-tab:: gdscript GDScript
  25. func _input(event):
  26. if event.is_action_pressed("jump"):
  27. jump()
  28. func _physics_process(delta):
  29. if Input.is_action_pressed("move_right"):
  30. # Move as long as the key/button is pressed.
  31. position.x += speed * delta
  32. .. code-tab:: csharp
  33. public override void _Input(InputEvent inputEvent)
  34. {
  35. if (inputEvent.IsActionPressed("jump"))
  36. {
  37. Jump();
  38. }
  39. }
  40. public override void _PhysicsProcess(float delta)
  41. {
  42. if (Input.IsActionPressed("move_right"))
  43. {
  44. // Move as long as the key/button is pressed.
  45. position.x += speed * delta;
  46. }
  47. }
  48. This gives you the flexibility to mix-and-match the type of input processing
  49. you do.
  50. For the remainder of this tutorial, we'll focus on capturing individual
  51. events in ``_input()``.
  52. Input events
  53. ------------
  54. Input events are objects that inherit from :ref:`InputEvent <class_InputEvent>`.
  55. Depending on the event type, the object will contain specific properties
  56. related to that event. To see what events actually look like, add a Node and
  57. attach the following script:
  58. .. tabs::
  59. .. code-tab:: gdscript GDScript
  60. extends Node
  61. func _input(event):
  62. print(event.as_text())
  63. .. code-tab:: csharp
  64. public override void _Input(InputEvent inputEvent)
  65. {
  66. GD.Print(inputEvent.AsText());
  67. }
  68. As you press keys, move the mouse, and perform other inputs, you'll see each
  69. event scroll by in the output window. Here's an example of the output:
  70. ::
  71. A
  72. InputEventMouseMotion : button_mask=0, position=(551, 338), relative=(-85, 47), speed=(0, 0)
  73. InputEventMouseButton : button_index=BUTTON_LEFT, pressed=true, position=(551, 338), button_mask=1, doubleclick=false
  74. InputEventMouseButton : button_index=BUTTON_LEFT, pressed=false, position=(551, 338), button_mask=0, doubleclick=false
  75. S
  76. F
  77. InputEventMouseMotion : button_mask=0, position=(547, 338), relative=(-1, 0), speed=(0, 0)
  78. InputEventMouseMotion : button_mask=0, position=(542, 338), relative=(-4, 0), speed=(0, 0)
  79. As you can see, the results are very different for the different types of
  80. input. Key events are even printed as their key symbols. For example, let's consider :ref:`InputEventMouseButton <class_InputEventMouseButton>`.
  81. It inherits from the following classes:
  82. - :ref:`InputEvent <class_InputEvent>` - the base class for all input events
  83. - :ref:`InputEventWithModifiers <class_InputEventWithModifiers>` - adds the ability to check if modifiers are pressed, such as :kbd:`Shift` or :kbd:`Alt`.
  84. - :ref:`InputEventMouse <class_InputEventMouse>` - adds mouse event properties, such as ``position``
  85. - :ref:`InputEventMouseButton <class_InputEventMouseButton>` - contains the index of the button that was pressed, whether it was a double-click, etc.
  86. .. tip:: It's a good idea to keep the class reference open while you're working
  87. with events so you can check the event type's available properties and
  88. methods.
  89. You can encounter errors if you try to access a property on an input type that
  90. doesn't contain it - calling ``position`` on ``InputEventKey`` for example. To
  91. avoid this, make sure to test the event type first:
  92. .. tabs::
  93. .. code-tab:: gdscript GDScript
  94. func _input(event):
  95. if event is InputEventMouseButton:
  96. print("mouse button event at ", event.position)
  97. .. code-tab:: csharp
  98. public override void _Input(InputEvent inputEvent)
  99. {
  100. if (inputEvent is InputEventMouseButton mouseEvent)
  101. {
  102. GD.Print($"mouse button event at {mouseEvent.Position}");
  103. }
  104. }
  105. InputMap
  106. --------
  107. The :ref:`InputMap <class_InputMap>` is the most flexible way to handle a
  108. variety of inputs. You use this by creating named input *actions*, to which
  109. you can assign any number of input events, such as keypresses or mouse clicks.
  110. A new Godot project includes a number of default actions already defined. To
  111. see them, and to add your own, open Project -> Project Settings and select
  112. the InputMap tab:
  113. .. image:: img/inputs_inputmap.png
  114. Capturing actions
  115. ~~~~~~~~~~~~~~~~~
  116. Once you've defined your actions, you can process them in your scripts using
  117. ``is_action_pressed()`` and ``is_action_released()`` by passing the name of
  118. the action you're looking for:
  119. .. tabs::
  120. .. code-tab:: gdscript GDScript
  121. func _input(event):
  122. if event.is_action_pressed("my_action"):
  123. print("my_action occurred!")
  124. .. code-tab:: csharp
  125. public override void _Input(InputEvent inputEvent)
  126. {
  127. if (inputEvent.IsActionPressed("my_action"))
  128. {
  129. GD.Print("my_action occurred!");
  130. }
  131. }
  132. Keyboard events
  133. ---------------
  134. Keyboard events are captured in :ref:`InputEventKey <class_InputEventKey>`.
  135. While it's recommended to use input actions instead, there may be cases where
  136. you want to specifically look at key events. For this example, let's check for
  137. the :kbd:`T`:
  138. .. tabs::
  139. .. code-tab:: gdscript GDScript
  140. func _input(event):
  141. if event is InputEventKey and event.pressed:
  142. if event.scancode == KEY_T:
  143. print("T was pressed")
  144. .. code-tab:: csharp
  145. public override void _Input(InputEvent inputEvent)
  146. {
  147. if (inputEvent is InputEventKey keyEvent && keyEvent.Pressed)
  148. {
  149. if ((KeyList)keyEvent.Scancode == KeyList.T)
  150. {
  151. GD.Print("T was pressed");
  152. }
  153. }
  154. }
  155. .. tip:: See :ref:`@GlobalScope_KeyList <enum_@GlobalScope_KeyList>` for a list of scancode
  156. constants.
  157. Keyboard modifiers
  158. ~~~~~~~~~~~~~~~~~~
  159. Modifier properties are inherited from
  160. :ref:`InputEventWithModifiers <class_InputEventWithModifiers>`. This allows
  161. you to check for modifier combinations using boolean properties. Let's imagine
  162. you want one thing to happen when the :kbd:`T` is pressed, but something
  163. different when it's :kbd:`Shift + T`:
  164. .. tabs::
  165. .. code-tab:: gdscript GDScript
  166. func _input(event):
  167. if event is InputEventKey and event.pressed:
  168. if event.scancode == KEY_T:
  169. if event.shift:
  170. print("Shift+T was pressed")
  171. else:
  172. print("T was pressed")
  173. .. code-tab:: csharp
  174. public override void _Input(InputEvent inputEvent)
  175. {
  176. if (inputEvent is InputEventKey keyEvent && keyEvent.Pressed)
  177. {
  178. switch ((KeyList)keyEvent.Scancode)
  179. {
  180. case KeyList.T:
  181. GD.Print(keyEvent.Shift ? "Shift+T was pressed" : "T was pressed");
  182. break;
  183. }
  184. }
  185. }
  186. .. tip:: See :ref:`@GlobalScope_KeyList <enum_@GlobalScope_KeyList>` for a list of scancode
  187. constants.
  188. Mouse events
  189. ------------
  190. Mouse events stem from the :ref:`InputEventMouse <class_InputEventMouse>` class, and
  191. are separated into two types: :ref:`InputEventMouseButton <class_InputEventMouseButton>`
  192. and :ref:`InputEventMouseMotion <class_InputEventMouseMotion>`. Note that this
  193. means that all mouse events will contain a ``position`` property.
  194. Mouse buttons
  195. ~~~~~~~~~~~~~
  196. Capturing mouse buttons is very similar to handling key events. :ref:`@GlobalScope_ButtonList <enum_@GlobalScope_ButtonList>`
  197. contains a list of ``BUTTON_*`` constants for each possible button, which will
  198. be reported in the event's ``button_index`` property. Note that the scrollwheel
  199. also counts as a button - two buttons, to be precise, with both
  200. ``BUTTON_WHEEL_UP`` and ``BUTTON_WHEEL_DOWN`` being separate events.
  201. .. tabs::
  202. .. code-tab:: gdscript GDScript
  203. func _input(event):
  204. if event is InputEventMouseButton:
  205. if event.button_index == BUTTON_LEFT and event.pressed:
  206. print("Left button was clicked at ", event.position)
  207. if event.button_index == BUTTON_WHEEL_UP and event.pressed:
  208. print("Wheel up")
  209. .. code-tab:: csharp
  210. public override void _Input(InputEvent inputEvent)
  211. {
  212. if (inputEvent as InputEventMouseButton mouseEvent && mouseEvent.Pressed)
  213. {
  214. switch ((ButtonList)mouseEvent.ButtonIndex)
  215. {
  216. case ButtonList.Left:
  217. GD.Print($"Left button was clicked at {mouseEvent.Position}");
  218. break;
  219. case ButtonList.WheelUp:
  220. GD.Print("Wheel up");
  221. break;
  222. }
  223. }
  224. }
  225. Mouse motion
  226. ~~~~~~~~~~~~
  227. :ref:`InputEventMouseMotion <class_InputEventMouseMotion>` events occur whenever
  228. the mouse moves. You can find the move's distance with the ``relative``
  229. property.
  230. Here's an example using mouse events to drag-and-drop a :ref:`Sprite <class_Sprite>`
  231. node:
  232. .. tabs::
  233. .. code-tab:: gdscript GDScript
  234. extends Node
  235. var dragging = false
  236. var click_radius = 32 # Size of the sprite
  237. func _input(event):
  238. if event is InputEventMouseButton and event.button_index == BUTTON_LEFT:
  239. if (event.position - $Sprite.position).length() < click_radius:
  240. # Start dragging if the click is on the sprite.
  241. if !dragging and event.pressed:
  242. dragging = true
  243. # Stop dragging if the button is released.
  244. if dragging and !event.pressed:
  245. dragging = false
  246. if event is InputEventMouseMotion and dragging:
  247. # While dragging, move the sprite with the mouse.
  248. $Sprite.position = event.position
  249. .. code-tab:: csharp
  250. public override void _Input(InputEvent inputEvent)
  251. {
  252. var sprite = GetNodeOrNull<Sprite>("Sprite");
  253. if (sprite == null)
  254. return;// No suitable node was found.
  255. if (inputEvent is InputEventMouseButton mouseEvent && (ButtonList)mouseEvent.ButtonIndex == ButtonList.Left)
  256. {
  257. if ((mouseEvent.Position - sprite.Position).Length() < clickRadius)
  258. {
  259. // Start dragging if the click is on the sprite.
  260. if (!dragging && mouseEvent.Pressed)
  261. dragging = !dragging;
  262. }
  263. // Stop dragging if the button is released.
  264. if (dragging && !mouseEvent.Pressed)
  265. {
  266. dragging = false;
  267. }
  268. }
  269. else
  270. {
  271. if (inputEvent is InputEventMouseMotion motionEvent)
  272. {
  273. // While dragging, move the sprite with the mouse.
  274. sprite.Position = motionEvent.Position;
  275. }
  276. }
  277. }
  278. Touch events
  279. ------------
  280. If you are using a touchscreen device, you can generate touch events.
  281. :ref:`InputEventScreenTouch <class_InputEventScreenTouch>` is equivalent to
  282. a mouse click event, and :ref:`InputEventScreenDrag <class_InputEventScreenDrag>`
  283. works much the same as mouse motion.
  284. .. tip:: To test your touch events on a non-touchscreen device, open Project
  285. Settings and go to the "Input Devices/Pointing" section. Enable "Emulate
  286. Touch From Mouse" and your project will interpret mouse clicks and
  287. motion as touch events.