Jump to content

Switching between KeyDown and MouseDown


Recommended Posts

So I want the players to be able to change the controls in the options menu, but I've encountered an interesting roadblock of which I'm not sure how to handle it.

Since the mouse is handled in separate methods in Window I can't simply pass the Key::whatever value, I also need to switch between the MouseDown or KeyDown function depending on what the setting chosen is.

Is there some clever way this can be done neatly?

Link to comment
Share on other sites

Can't you just do "KeyDown(x) or MouseDown(x)"? I don't know much about Lua.

You could store the button or key as "M0" or "K62," respectively, and check for an "M" or "K" (that's one way I have done it in other applications)

i now hate love C++

Beeeeeeeeeeeeeep~~This is a test of the emergency signature system~~Beeeeeeeeeeeeeep

RX 6800XT | i5-13600KF | 32GB DDR5 | 1440p is perfect

Link to comment
Share on other sites

I've solved it now by using a bool for every control which sets if the control is a mouse or keyboard control (i.e. 'mouse_throw' for 'key_throw' etc).
After that it's just checking like this:

(window->KeyHit(key_throw) && !mouse_throw) || (window->MouseHit(key_throw) && mouse_throw)

And this works fine. Just wondering if there's other ways of doing it :)

Link to comment
Share on other sites

You could also try another approach:

Pseudocode:

class GameAction
{
	public:
		virtual bool IsCalled() = 0;
}

class KeyHitAction : GameAction
{
	private int key;
	private Window* window;
	public:
		KeyAction(Window* window, int key);
		virtual bool IsCalled() { return window->KeyHit(key);}
}

class MouseHitAction : GameAction
{
	private int button;
	private Window* window;
	public:
		KeyAction(Window* window, int button);
		virtual bool IsCalled() { return window->MouseHit(button);}
}

std:map<string,GameAction*> actions;
actions["THROW"] = KeyHitAction(window,KEY_T);
actions["SELECT"] = MouseHitAction(window,MouseButton::Left);
  
  //in Loop
  if(actions["THROW"]->IsCalled())
  {
  }

This could easily to be extended to Gamepads and other input devices. Also you could add MouseDownActions or other types of Actions.

  • Like 1
  • Intel® Core™ i7-8550U @ 1.80 Ghz 
  • 16GB RAM 
  • INTEL UHD Graphics 620
  • Windows 10 Pro 64-Bit-Version
Link to comment
Share on other sites

You could store the mouse button value as a negative number in the settings, and then use MouseDown(-key) if the key value is negative, or KeyDown(key) if it is positive.

  • Like 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...