This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Sammich Lord's profile picture

Mouse Help

Started by Sammich Lord, 12 October 2012 - 06:31 AM
Sammich Lord #1
Posted 12 October 2012 - 08:31 AM
I am trying to make a program to constantly wait for mouse input then compare it to the table.
However it will not work.
Please ignore the "she" function I was going to parallel with shell after it worked but It wouldn't work.
Remember most the code was me trying to debug it.
I would really love if you guys could explain the best way to do it.

local mouse = {
  [1] = {buttonX = 1, buttonY = 2, command = "print('Hi')"}
}
local function she()
  shell.run("shell")
end
local function getMouse()
  while true do
    event, p1, p2, p3 = os.pullEvent()
    if event == "click" then
	  for k, v in ipairs(mouse) do
	    if mouse[k].buttonX == p1 and mouse[k].buttonY == p2 then
		  print("hi")
	    end
	  end
    end
  end
end
getMouse()
Luanub #2
Posted 12 October 2012 - 08:34 AM
What version on you running this on? The mouse clicks only work in the current beta of 1.45 pr1. If you're using that and having issues you should post that in the beta section of the forums and not here.
Sammich Lord #3
Posted 12 October 2012 - 08:37 AM
What version on you running this on? The mouse clicks only work in the current beta of 1.45 pr1. If you're using that and having issues you should post that in the beta section of the forums and not here.
I am using the beta however this is a VERY general question. I am asking how to compare a var to a var in a table.
Luanub #4
Posted 12 October 2012 - 08:49 AM
It depends on how the table is formated. I think you're table is formatted wrong. But not sure I will have to play with it some. For a standard table that has it values assigned like tTable[1], and not tTable[putWhatYouWantHere] then you can simply do:

tTable = {1,2}

event, p1, p2, p3 = os.pullEvent()
  if p2 == tTable[1] and p3 == tTable[2] then
	 --it matches
  else
	--it does not match
  end
end

I think your using a custom setup like tTable[whatYouWantHere], which using ipairs in the for loop should be correct. I will play with it more and see if I cant find your problem


EDIT: Part of the problem I noticed is that your checking p1 which is the event type so for a mouse click p1 will == "click"
Edited on 12 October 2012 - 06:51 AM
Sammich Lord #5
Posted 12 October 2012 - 09:02 AM
It depends on how the table is formated. I think you're table is formatted wrong. But not sure I will have to play with it some. For a standard table that has it values assigned like tTable[1], and not tTable[putWhatYouWantHere] then you can simply do:

tTable = {1,2}

event, p1, p2, p3 = os.pullEvent()
  if p2 == tTable[1] and p3 == tTable[2] then
	 --it matches
  else
	--it does not match
  end
end

I think your using a custom setup like tTable[whatYouWantHere], which using ipairs in the for loop should be correct. I will play with it more and see if I cant find your problem


EDIT: Part of the problem I noticed is that your checking p1 which is the event type so for a mouse click p1 will == "click"
Thanks for the help!
But correction! The "click" event returns p1 as x, p2 as y and p3 as the mouse button such as 1,2,3,4,5.
Luanub #6
Posted 12 October 2012 - 09:33 AM
lol ya I realized that on my way home from work. My brain is fried after a long boring night at work with nothing to do…
Luanub #7
Posted 12 October 2012 - 11:23 AM
So its weird. The first time I ran it I got a weird error about a number with a ?. Then I commented out some stuff, did a whole bunch of debugging that told me that it should work as is… Removed the debug and comments and it works…

Try it again..?? It seems to work fine for me.
PonyKuu #8
Posted 12 October 2012 - 01:14 PM
Doesn't os.pullEvent() return an event type as the first value? If you make something like this:


local event, sender, message = os.pullEvent ("rednet_message")

event always will be equal to the string "rednet_message"…
Sammich Lord #9
Posted 12 October 2012 - 03:32 PM
Doesn't os.pullEvent() return an event type as the first value? If you make something like this:


local event, sender, message = os.pullEvent ("rednet_message")

event always will be equal to the string "rednet_message"…
The point of this is to run in the background so you can have multiple buttons but also do other stuff.
PonyKuu #10
Posted 12 October 2012 - 03:54 PM
Oh, I'm sorry, I have misread the code…
GopherAtl #11
Posted 12 October 2012 - 04:13 PM
What version on you running this on? The mouse clicks only work in the current beta of 1.45 pr1. If you're using that and having issues you should post that in the beta section of the forums and not here.

I'm not an authority of any sort but I'm pretty sure the beta forum is for formal bug reports ONLY, not for support on how to use the new features.

I'd guess help q's like this belong in this subforum, but they should have titles starting with [1.45pr1], or whatever version they apply to.