Jump to content

A More Robust Input command


Einlander
 Share

Recommended Posts

While programming it became an increasing hassle to key track of what variable was a mouse button and what command was from the keyboard. So I wrote a function that will treat the mouse and the keyboard as the same input.

 


-- This creates a function that is capable of handeling both keyboard and mouse input

function InputHit(keycode)

local window = Window:GetCurrent()

local context=Context:GetCurrent()

if keycode == Key.LButton then return window:MouseHit(Key.LButton) end

if keycode == Key.RButton then return window:MouseHit(Key.RButton) end

if keycode == Key.MButton then return window:MouseHit(Key.MButton) end

return window:KeyHit(keycode)

end

function InputDown(keycode)

local window = Window:GetCurrent()

local context=Context:GetCurrent()

if keycode == Key.LButton then return window:MouseDown(Key.LButton) end

if keycode == Key.RButton then return window:MouseDown(Key.RButton) end

if keycode == Key.MButton then return window:MouseDown(Key.MButton) end

return window:KeyDown(keycode)

end

 

I'm still trying to figure out how to get the mouse wheel without interrupting anything

Link to comment
Share on other sites

What do you mean with "without interrupting anything"?

You can get the mouse wheel as the z-coordinate of the GetMousePosition()-function

(http://www.leadwerks.com/werkspace/page/documentation/_/command-reference/window/windowgetmouseposition-r455)

 

What if i just want to poll if the mouse wheel is changed . Now I have introduce a global that will keep track of the change since the getmouseposition remembers its state. This brings up some complications. Is this the first time the function has been called? Does the value change even when I'm not testing for it? If this is true then how do I handle a non zero z value? do I say it didn't move or that it moved? Lots of problems can arise.

Link to comment
Share on other sites

I guess I never really considered this a problem needing solving. Maybe because I'm just used to using LE for so many years but now that I see these functions it does seem like it would be easier to work with. Thanks for sharing.

 

I've been writing my own FPS controller and I have abstracted away the key commands. I load the key config from a file into a named table. To test for a key I do:

if InputHit(keybinds.attack) then

Since the key is user defined it can be a mouse button (in this case it is the left mouse button). This allows me to have 1 command to test for both mouse and keyboard. I'm experimenting with the mouse wheel.

Link to comment
Share on other sites

I needed to do this for zooming the camera in/out on the player.

 

Script.mouseTemp = Vec3()


function Script:GetMouseWheel()
local context = Context:GetCurrent()
local window = context:GetWindow()
local mouseWheelPos = window:GetMousePosition()
if ( mouseWheelPos.z > self.mouseTemp.z ) then
 self.mouseTemp = mouseWheelPos
 System:Print("Return 1 for Wheel up")
 return 1
elseif ( mouseWheelPos.z < self.mouseTemp.z ) then
 self.mouseTemp = mouseWheelPos
 System:Print("Return 2 for Wheel down")
 return 2
end
self.mouseTemp.z = mouseWheelPos.z
--if wheel not moved it returns 0
return 0
end

---

Scott

 

Using Windows 7 Ultimate 64 bit/Core I7-2700K @ 4312mhz/24G RAM/Nvidia GTX 1060

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...