RMRK is retiring.
Registration is disabled. The site will remain online, but eventually become a read-only archive. More information.

RMRK.net has nothing to do with Blockchains, Cryptocurrency or NFTs. We have been around since the early 2000s, but there is a new group using the RMRK name that deals with those things. We have nothing to do with them.
NFTs are a scam, and if somebody is trying to persuade you to buy or invest in crypto/blockchain/NFT content, please turn them down and save your money. See this video for more information.
[GM] buttons with Game controllers

0 Members and 1 Guest are viewing this topic.

**
Rep:
Level 88
I'm scared of my grandma. Wouldn't you be?
I am making a game that uses a game controller. I know which buttons correspond to event comands, but I need to know how to find out if the player has released them, pressed them, or is holding them. I looked in the help file for GM language coding, but none seemed to do what I wanted. Please help
« Last Edit: January 04, 2012, 10:54:43 PM by modern algebra »

********
Rep:
Level 96
2011 Most Missed Member2010 Zero To Hero
Well, I think you would want to look for something along the lines of "keyboard_check(-key-)", but a command that would apply to joypads, and check a "not" box. I'll help more shortly, but I'm going to be somewhat busy for the next hour or so.

*
Shooting for the "MEMBER MODERATOR OF THE YEAR 2007" Award
Rep:
Level 89
Hi, there.
Key GML Codes

For keyboard interaction, the following variables and functions exist:

keyboard_lastkey Keycode of last key pressed. See below for keycode constants. You can change it, e.g. set it to 0 if you handled it.
keyboard_key Keycode of current key pressed (see below; 0 if none).
keyboard_lastchar Last character pressed (as string).
keyboard_string String containing the last at most 1024 characters typed. This string will only contain the printable characters typed. It also correctly responds to pressing the backspace key by erasing the last character.

Sometimes it is useful to map one key to another. For example you might want to allow the player to use both the arrow keys and the numpad keys. Rather than duplicating the actions you can map the numpad keys to the arrow keys. Also you might want to implement a mechanism in which the player can set the keys to use. For this the following functions are available:


keyboard_set_map(key1,key2) Maps the key with keycode key1 to key2.
keyboard_get_map(key) Returns the current mapping for key.
keyboard_unset_map() Resets all keys to map to themselves.

To check whether a particular key or mouse button is pressed you can use the following functions. This is in particular useful when multiple keys are pressed simultaneously.


keyboard_check(key) Returns whether the key with the particular keycode is currently down.
keyboard_check_pressed(key) Returns whether the key with the particular keycode was pressed since the last step.
keyboard_check_released(key) Returns whether the key with the particular keycode was released since the last step.
keyboard_check_direct(key) Returns whether the key with the particular keycode is pressed by checking the hardware directly. The result is independent of which application has focus. It allows for a few more checks. In particular you can use keycodes vk_lshift, vk_lcontrol, vk_lalt, vk_rshift, vk_rcontrol and vk_ralt to check whether the left or right shift, control or alt key is pressed.

The following routines can be used to manipulate the keyboard state:


keyboard_get_numlock() Returns whether the numlock is set.
keyboard_set_numlock(on) Sets (true) or unsets (false) the numlock.
keyboard_key_press(key) Simulates a press of the key with the indicated keycode.
keyboard_key_release(key) Simulates a release of the key with the indicated keycode.

The following constants for virtual keycodes exist:


vk_nokey keycode representing that no key is pressed
vk_anykey keycode representing that any key is pressed
vk_left keycode for left arrow key
vk_right keycode for right arrow key
vk_up keycode for up arrow key
vk_down keycode for down arrow key
vk_enter enter key
vk_escape escape key
vk_space space key
vk_shift shift key
vk_control control key
vk_alt alt key
vk_backspace backspace key
vk_tab tab key
vk_home home key
vk_end end key
vk_delete delete key
vk_insert insert key
vk_pageup pageup key
vk_pagedown pagedown key
vk_pause pause/break key
vk_printscreen printscreen/sysrq key
vk_f1 ... vk_f12 keycodes for the function keys F1 to F12
vk_numpad0 ... vk_numpad9 number keys on the numeric keypad
vk_multiply multiply key on the numeric keypad
vk_divide divide key on the numeric keypad
vk_add add key on the numeric keypad
vk_subtract subtract key on the numeric keypad
vk_decimal decimal dot keys on the numeric keypad

For the letter keys use for example ord('A'). (The capital letters.) For the digit keys use for example ord('5') to get the <5> key. The following constants can only be used in keyboard_check_direct:


vk_lshift left shift key
vk_lcontrol left control key
vk_lalt left alt key
vk_rshift right shift key
vk_rcontrol right control key
vk_ralt right alt key

There are some additional functions related to keyboard interaction.


keyboard_clear(key) Clears the state of the key. This means that it will no longer generate keyboard events until it starts repeating.
io_clear() Clears all keyboard and mouse states.
io_handle() Handle user io, updating keyboard and mouse status.
keyboard_wait() Waits till the user presses a key on the keyboard.

D&D Codes

Keyboard events
When the player presses a key, a keyboard event happens for all instances of all objects. There is a different event for each key. In the menu you can pick the key for which you want to define the keyboard event and next drag actions there. Clearly, only a few objects need events for only a few keys. You get an event in every step as long as the player keeps the key depressed. There are two special keyboard events. One is called <No key>. This event happens in each step when no key is pressed. The second one is called <Any key> and happens whatever key is pressed. When the player presses multiple keys, the events for all the keys pressed happen. Note that the keys on the numeric keypad only produce the corresponding events when <NumLock> is pressed.

 Key press events
This event is similar to the keyboard event but it happens only once when the key is pressed, rather than continuously. This is useful when you want an action to happen only once.

 Key release eventsThis event is similar to the keyboard event but it happens only once when the key is released, rather than continuously.

Hope this helps
Sig by MacGravel

*
Shooting for the "MEMBER MODERATOR OF THE YEAR 2007" Award
Rep:
Level 89
Hi, there.
Joystick GML Codes

There are some events associated with joysticks. But to have full control over the joysticks there is a whole set of functions to deal with joysticks. Game Maker supports up to two joysticks. So all of these functions take a joystick id as argument.

joystick_exists(id) Returns whether joystick id (1 or 2) exists.
joystick_name(id) Returns the name of the joystick
joystick_axes(id) Returns the number of axes of the joystick.
joystick_buttons(id) Returns the number of buttons of the joystick.
joystick_has_pov(id) Returns whether the joystick has point-of-view capabilities.
joystick_direction(id) Returns the keycode (vk_numpad1 to vk_numpad9) corresponding to the direction of joystick id (1 or 2).
joystick_check_button(id,numb) Returns whether the joystick button is pressed (numb in the range 1-32).
joystick_xpos(id) Returns the position (-1 to 1) of the x-axis of joystick id.
joystick_ypos(id) Returns the joysticks y-position.
joystick_zpos(id) Returns the joysticks z-position (if it has a z-axis).
joystick_rpos(id) Returns the joysticks rudder position (or fourth axis).
joystick_upos(id) Returns the joysticks u-position (or fifth axis).
joystick_vpos(id) Returns the joysticks v-position (or sixth axis).
joystick_pov(id) Returns the joysticks point-of view position. This is an angle between 0 and 360 degrees. 0 is forwards, 90 to the right, 180 backwards and 270 to the left. When no point-of-view direction is pressed by the user -1 is returned.

Hope this helps
Sig by MacGravel

**
Rep:
Level 88
I'm scared of my grandma. Wouldn't you be?
Thanks for trying, but I had already tried the joystick_check_button(id), but it only read if it was being held. I did, however, figure out a way since. It's a lot of work but it could help some people.
First you make a script, then I put this in it.

if global.button_1A = 1
{global.button_1A_pressed += 1}
else
{
 if global.button_1A = 0
 {
  global.button_1A_pressed = 0
  }
 }

You can then use other code blocks to ask
if global.button_1A_pressed = 1
 {action}
It's a pain to do but it works. You have to make a script for each button and change the global variable. I'll try to post an example later if anyone needs it.

********
Rep:
Level 96
2011 Most Missed Member2010 Zero To Hero
No no no, like this:

If joystick_check_button(id,numb) NOT

Or something like that. When you use a not statement, it reverses the question. So in this case, instead of asking if it's pressed, it asks if it's NOT pressed.

*
Shooting for the "MEMBER MODERATOR OF THE YEAR 2007" Award
Rep:
Level 89
Hi, there.
Here you go.

http://gmc.yoyogames.com/index.php?showtopic=22469

This explains how to set up, script, and get the joystick to work.
Sig by MacGravel