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

How to become acquainted with the mouse functions

Started by AnthonyD98™, 29 January 2013 - 01:53 PM
AnthonyD98™ #1
Posted 29 January 2013 - 02:53 PM
Hello I need a some help on becoming acquainted with using mouse functions (mouse_click)

I want to know how to use the mouse_click function in programs.
- How to use it
- What I can use it for
- What it can do
- Example Codes / Tutorials

Thanks, AnthonyD98
theoriginalbit #2
Posted 29 January 2013 - 02:58 PM
- How to use it: The Wiki is our friend. :)/> http://computercraft...se_click_(event)
- What I can use it for: You use it for detecting mouse clicks on the screen, i.e. GUI's :blink:/> :blink:/> :blink:/>
- What it can do: Tell you the mouse was clicked… an event doesn't do anything, ever, you need to do stuff with it :blink:/> :blink:/> :blink:/>
- Example Codes / Tutorials: Again, the Wiki and here in the "Tutorials" section: http://www.computercraft.info/forums2/index.php?/topic/7526-the-mouse-events/
Zoinky #3
Posted 29 January 2013 - 03:03 PM
The mouse_click event is fired when you click either mouse key, you can catch the event using os.pullEvent("mouse_click"), it will return the mouse button that was clicked, the x coordinate of the click and the y coordinate of the click. You can use it to create buttons for certain programs. It can tell you where the mouse button was pushed down, but it will not keep updating. You need to use the mouse_drag event to keep track of where the user is moving the cursor. Example:


local event, button, x, y, = os.pullEvent("mouse_click") -- Catches the mouse_click event
term.setCursorPos(x,y) -- Sets the x, y coordinates to where you clicked
print(" < You clicked here!") -- Prints this string at that location

As TheOriginalBIT said, it's easier to just look at the wiki. It has plenty of info on most subjects.
theoriginalbit #4
Posted 29 January 2013 - 03:08 PM

local event, button, x, y = os.pullEvent()
if event == "mouse_click" then
  -- it was clicked
end
Also to expand on Zoinky since SO many people don't seem to understand this part. Where Zoinky has

local event, button, x, y = os.pullEvent("mouse_click")
the program will WAIT there UNTIL the user clicks the mouse. It WILL NOT continue running. To have it not do this you would do something like this

local event, button, x, y = os.pullEvent()
if event == "mouse_click" then
  -- it was clicked
end
doing the above method also allows us to check for multiple events like so

local event, button, x, y = os.pullEvent()
if event == "mouse_click" then
  -- it was clicked
elseif event == "mouse_drag" then
  -- the mouse was dragged
end
Edited on 29 January 2013 - 02:10 PM
AnthonyD98™ #5
Posted 29 January 2013 - 03:12 PM
How would I make it do something If it clicked a specific x and y location?
theoriginalbit #6
Posted 29 January 2013 - 03:15 PM
Again all on the Wiki and the tutorial pages……..

local event, button, x, y = os.pullEvent()
if event == "mouse_click" then
  if button == 1 then -- this is left click, 2 is right click and 3 is middle click
	if x == 1 and y == 1 then -- top left corner clicked
	  -- some 1 pixel button or such
	elseif x >= 2 and x <= 5 and y >= 2 and y <= 4 then
	  -- a larger area
	end
  end
end
Zoinky #7
Posted 29 January 2013 - 03:17 PM
How would I make it do something If it clicked a specific x and y location?


if event == "mouse_click" and x == XPOS and y == YPOS then -- Replace XPOS and YPOS with the location you want.
-- Do stuff
end

Use that code after you catch the event.

Edit: Ninja'd. Lol :P/>
AnthonyD98™ #8
Posted 29 January 2013 - 03:21 PM
Again all on the Wiki and the tutorial pages……..

local event, button, x, y = os.pullEvent()
if event == "mouse_click" then
  if button == 1 then -- this is left click, 2 is right click and 3 is middle click
	if x == 1 and y == 1 then -- top left corner clicked
	  -- some 1 pixel button or such
	elseif x >= 2 and x <= 5 and y >= 2 and y <= 4 then
	  -- a larger area
	end
  end
end

What does the
elseif x >= 2 and x <= 5 and y >= 2 and y <= 4 then
do?
theoriginalbit #9
Posted 29 January 2013 - 03:22 PM
it detects a larger area click.
so if the mouse was clicked within x 2-5 and y 2-4 then do this. its might be a big button there or such.
AnthonyD98™ #10
Posted 29 January 2013 - 03:29 PM
Okay thanks guys I kind have an understanding I will try write a basic program using your codes.