How to check for both keyboard and mouse?

W

WolfHybrid23

Guest
Hello, I am making a game and I want the controls to be changeable by the user, I am not new to GameMaker: Studio, In fact on a scale from 1 to 10 I would say that I am around a 9, I need help with something I need to check for either keyboard or mouse input. The reason this is is because if the user wants to change a control to be a mouse button then I want to check for a mouse button rather than check for a keyboard button (and vice versa).
 

samspade

Member
Hello, I am making a game and I want the controls to be changeable by the user, I am not new to GameMaker: Studio, In fact on a scale from 1 to 10 I would say that I am around a 9, I need help with something I need to check for either keyboard or mouse input. The reason this is is because if the user wants to change a control to be a mouse button then I want to check for a mouse button rather than check for a keyboard button (and vice versa).
I'm not really sure what your asking. Do you just want something like:

click = keyboard_check_pressed(vk_enter) || mouse_check_button_pressed(mb_left)?

A lot depends upon how you've set other things up. For example, if it is a menu and you can use the keyboard or click on with the mouse then you might want to do something like this:

select = keyboard_check_pressed(vk_enter) || (place_meeting(mouse_x, mouse_y, obj_menu_button) && mouse_check_button_pressed(mb_left));

Or do you want to accept only keyboard or mouse but not both at the same time? Or are you asking how to 'rebind' controls?
 
W

WolfHybrid23

Guest
I'm not really sure what your asking. Do you just want something like:

click = keyboard_check_pressed(vk_enter) || mouse_check_button_pressed(mb_left)?

A lot depends upon how you've set other things up. For example, if it is a menu and you can use the keyboard or click on with the mouse then you might want to do something like this:

select = keyboard_check_pressed(vk_enter) || (place_meeting(mouse_x, mouse_y, obj_menu_button) && mouse_check_button_pressed(mb_left));

Or do you want to accept only keyboard or mouse but not both at the same time? Or are you asking how to 'rebind' controls?
Well, Let me give you some background on this game, I am making it for desktop, it's a fan game for a DS game and I want the experience to be like a ds, so instead of doing normal controls I am basing all of the controls of the game on the original ds buttons here is what I am talking about:
Code:
enum buttons{
    a, b, x, y, left, right, up, down, left_trigger, right_trigger, start, select, volume_up, volume_down, screen_touch
}
globalvar Buttons;
Buttons[buttons.a] = ord("D");
Buttons[buttons.b] = ord("S");
///ect until here
Buttons[buttons.screen_touch] = mb_any;
But in case the user changes the screen_touch control to a keyboard key, How do I handle that, The documentation says nothing about the function throwing an error if the key is not a key with the function "keyboard_check_*" but I just want to be sure.
 
W

WolfHybrid23

Guest
And I have am making a script that checks for the buttons.
Code:
/// @function button_check_pressed( Button )
/// @argument Button - The button id to check for
return keyboard_check_pressed( Buttons[argument0] );
But I don't know how it would react to a mouse constant aside from a keyboard constant or character byte.
 
W

WolfHybrid23

Guest
Maybe it would help if I understood what numerical value was assigned to these constants?
 
W

WolfHybrid23

Guest
Ah, yes, Just debug messaged the constants values and I believe I have a solution to this:
The unicode for "A" was 65, and for "Z" was 90 and the value for mb_left was 1 and mb_any was -1 so in theory if I check to see if the value in in those ranges and switch between mouse_check and keyboard_check.
 

samspade

Member
And I have am making a script that checks for the buttons.
Code:
/// @function button_check_pressed( Button )
/// @argument Button - The button id to check for
return keyboard_check_pressed( Buttons[argument0] );
But I don't know how it would react to a mouse constant aside from a keyboard constant or character byte.
You can assign numerical values to enums if you want. They "can only be real numbers or expressions with previous enums, and by default are numbered from 0 upwards." See the help document for examples. keyboard_check_pressed returns true or false. I believe ord("D") and the like return ascii values though I'm not positive. I don't know what mb_any or any of the other mb_left etc return. keyboard_check_pressed does not work with mb_* constants so your script will not work as it is.

However, you could fairly easily modify your script as noted in my initial reply to work. I believe even something like:

Code:
/// @function button_check_pressed( Button )
/// @argument Button - The button id to check for
return keyboard_check_pressed( Buttons[argument0] ) || (place_meeting(mouse_x, mouse_y, obj_menu_button) && mouse_check_button_pressed(mb_left));
Would work assuming that the game was set up so that place meeting would work. You can easily use the idea there to modify it however you like.
 
W

WolfHybrid23

Guest
You can assign numerical values to enums if you want. They "can only be real numbers or expressions with previous enums, and by default are numbered from 0 upwards." See the help document for examples. keyboard_check_pressed returns true or false. I believe ord("D") and the like return ascii values though I'm not positive. I don't know what mb_any or any of the other mb_left etc return. keyboard_check_pressed does not work with mb_* constants so your script will not work as it is.

However, you could fairly easily modify your script as noted in my initial reply to work. I believe even something like:

Code:
/// @function button_check_pressed( Button )
/// @argument Button - The button id to check for
return keyboard_check_pressed( Buttons[argument0] ) || (place_meeting(mouse_x, mouse_y, obj_menu_button) && mouse_check_button_pressed(mb_left));
Would work assuming that the game was set up so that place meeting would work. You can easily use the idea there to modify it however you like.
Did you even read my previous post, I was using enumerators. Or did you just read it and tell me what I already did to try and make others think I'm dumb or something?
 
W

WolfHybrid23

Guest
Whatever it doesn't matter I fixed the issue already.
 

samspade

Member
Did you even read my previous post, I was using enumerators. Or did you just read it and tell me what I already did to try and make others think I'm dumb or something?
I apologize. I hadn't read the two posts previous to my last post as I was typing that post at the time. I'm glad you solved it.
 
W

WolfHybrid23

Guest
I apologize. I hadn't read the two posts previous to my last post as I was typing that post at the time. I'm glad you solved it.
I can tell you how I solved it. So the keyboard input beings at unicode 32 while mouse input begins at -1 and ends at 2 so I checked if the value was below 32 and if so made the game check for mouse input rather than keyboard input and if not it checks for keyboard input
Code:
/// @function button_check_pressed( Button )
/// @argument Button - The button to check for (Use the enumerator buttons to reference the button)
if(Buttons[argument0] < 32)return mouse_check_button_pressed( Buttons[argument0] );
else return keyboard_check_pressed( Buttons[argument0] );
 
D

dirkinz

Guest
I can tell you how I solved it. So the keyboard input beings at unicode 32 while mouse input begins at -1 and ends at 2 so I checked if the value was below 32 and if so made the game check for mouse input rather than keyboard input and if not it checks for keyboard input
Code:
/// @function button_check_pressed( Button )
/// @argument Button - The button to check for (Use the enumerator buttons to reference the button)
if(Buttons[argument0] < 32)return mouse_check_button_pressed( Buttons[argument0] );
else return keyboard_check_pressed( Buttons[argument0] );
Not to throw a wrench in your plans but I was trying to figure out the same issue and as it turns out some of the keyboard constants do go below 32.
vk_alt => 18
vk_anykey => 1
vk_backspace => 8
vk_control => 17
vk_enter => 13
vk_escape => 27
vk_nokey => 0
vk_pause => 19
vk_return => 13
vk_shift => 16
vk_space => 32
vk_tab => 9

This means that:
Code:
mouse_check_button(vk_anykey ) // returns true if the left mouse button is pressed - vk_anykey = 1 = mb_left
mouse_check_button(vk_nokey) // returns true if no mouse buttons are being pressed - vk_nokey = 0 = mb_none
Also:
Code:
keyboard_check(mb_none) // returns true if no key is being pressed - mb_none = 0 = vk_nokey
however
Code:
keyboard_check_direct(mb_none) // never returns true as no individual key has a id of 0
With all that said, your logic should hold true with a couple modifications.
  1. Rather then checking if(Buttons[argument0] < 32) you should check if(Buttons[argument0] <= 3) as the highest mouse button is id:3.
  2. Additionally, this code snippet can produce undesired results if its ever passed a key grouping (ie. vk_anykey or mb_none) so only use it if you don't need to use those groups
 
Top