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

[Tutorial] How To Use Mouse Function In Advanced Computers

Started by Skullblade, 28 October 2012 - 03:51 PM
Skullblade #1
Posted 28 October 2012 - 04:51 PM
Computercraft 1.45 has rolled out and with it there are new functions!
One of the coolest features is the mouse support but sadly there is VERY little documentation of what you do to use it. In this tutorial I will show you how to use mouse support and give you a small sample code!

Here is the code
Spoilerwhile true do – Creates a loop
local event, button, X, Y = os.pullEvent("mouse_click") – Gets data for click(Like read() but for mice)
–X Quards and Y Quards

XY = X..","..Y – makes the mouse_click in X,Y format, other wise you have separate if statements for X and Y

if XY == "Position" then – Position is the Quards of were you want to click (X,Y format)
– Do Stuff
break – Stops program loop
end
end

Use Left and Right Click Differently
Spoilerwhile true do
local event, button, X, Y = os.pullEvent("mouse_click")
XY = mouseX..","..mouseY

if XY == "Position" and button == 1 then –Button is what click; 1 or 2…1 is Left, 2 is Right
–Do Stuff for Left Click (ie run program)
break
end

if XY == "Position" and button == 2 then
– Do Stuff for Right Click
break
end
end

Here is a example program
Spoilerprint ("Menu-Click the Number")
print ("1.Option 1")
print ("2.Option 2")
print ("3.Option 3")

while true do
local event, button, X, Y = os.pullEvent("mouse_click")
XY = X..","..Y

if XY == "1,2" and button == 1 then
– Do stuff for Option 1- Left Click
break
end


if XY == "1,3" and button == 1 then
– Do stuff for Option 2- Left Click
break
end


if XY == "1,4" and button == 1 then
– Do stuff for Option 3- Left Click
break
end

if XY == "1,2" and button == 2 then
– Do stuff for Option 1- Right Click
break
end


if XY == "1,3" and button == 2 then
– Do stuff for Option 2- Right Click
break
end


if XY == "1,4" and button == 2 then
– Do stuff for Option 3- Right Click
break
end
end


I'm new to the forums guys so if this helped you please leave a comment or if you have any suggestions plz leave them:)
ChunLing #2
Posted 28 October 2012 - 07:00 PM
Now I have somewhere to point everyone who asks about mouse click events. Though it would be nice if you could not restrict the pulls to mouse clicks, so that people could user other input.
Skullblade #3
Posted 28 October 2012 - 07:25 PM
You probably can but as i said this is just a basic program and there is ALOT of room for improvement. I just wanted to show everyone a starting point for mouse clicks
aSdtIHN0dXBpZA== #4
Posted 30 October 2012 - 12:24 PM
Is it possible to detect right clicks, say if you right-click on a virtual icon, you can bring up a tiny menu, instead of doing something else?

Spoilerthatpost = "HelpJedi"
if thatpost == "HelpJedi" then
print("Skullblade rocks! Literally!")
break
end
Jasonfran #5
Posted 30 October 2012 - 01:33 PM
Is it possible to detect right clicks, say if you right-click on a virtual icon, you can bring up a tiny menu, instead of doing something else?

Spoilerthatpost = "HelpJedi"
if thatpost == "HelpJedi" then
print("Skullblade rocks! Literally!")
break
end

The mouse click returns the button too, as you see in the first bit of code. You just check if button == 2.

Left click is 1 right click is 2
Skullblade #6
Posted 30 October 2012 - 02:39 PM
yeah Jason is right…i don't use that function much so i didn't include it but I will.

Hit by the hurricane:( lost power…running off battery:(
XeriosValentine #7
Posted 04 December 2012 - 03:44 AM
Nice starthelper :D/>
It's always better to give a hint than a whole program cuz with a program nobody learn how to :P/>
THX for hint I'll use it wisely 8)
nutcase84 #8
Posted 25 December 2012 - 04:13 AM
Thank you for this. It will help lots.
Crowdy199 #9
Posted 16 January 2013 - 10:34 AM
can u guys give me an example program of draging it like if they hold right down starting at 7,7 to 9,9 then do this code if you guys could do that that would be great thanks
theoriginalbit #10
Posted 16 January 2013 - 11:27 AM
Good job… nice and simple tutorial… code tags?
xxxredportalxxx #11
Posted 16 January 2013 - 07:19 PM
Wow thx for the tutorial ive been trying to work this out for some time now, all be useing this in my projects :D/>
TheBigJoker #12
Posted 18 January 2013 - 03:43 AM
Hey, nice program but I have an issue, I am trying to use this as a simple Button to open my door, but making it last for 5 seconds. The problem is, once I click the button once, everything works but then I have to run the program again to make it work.
zekesonxx #13
Posted 18 January 2013 - 04:31 AM
Computer != CPU.

A CPU is a part of the computer. It is a chip, placed in the motherboard, that does all of the figuring out of data.
A computer contains many parts, such as RAM, CPU, motherboard, and a few others.
Skullblade #14
Posted 19 January 2013 - 09:48 AM
1.) Shhhiiiiiit this is SUCH a old post I thought It died years ago;)
2.)put the program in a while true do loop to click more then once
3.)CPU is not computer I know that…it's a bad habit that I got from when I was a kid and on some of the game boy games I played they would call the computer opponent would ba called the CPU (not sure why)…I know that a CPU is a central processing unit and its were all of the computer calculations and such take place. It's just a bad habit
ChunLing #15
Posted 19 January 2013 - 03:21 PM
CPU controlled opponents are commonly referred to as CPU in a lot of games (including in source code). The reason is fairly obvious…while all the "players" are controlled by the computer, those controlled by input from the i/o interface are the actual players and those controlled by the CPU are the AI players.

While the CC computer blocks are complete computers in themselves, they take enough peripherals that I didn't blink at you calling them CPUs.
Skullblade #16
Posted 19 January 2013 - 05:41 PM
CPU controlled opponents are commonly referred to as CPU in a lot of games (including in source code). The reason is fairly obvious…while all the "players" are controlled by the computer, those controlled by input from the i/o interface are the actual players and those controlled by the CPU are the AI players.

While the CC computer blocks are complete computers in themselves, they take enough peripherals that I didn't blink at you calling them CPUs.
boy you put a lot more thought into that then I do when I just write CPU…it is a bad habit that I don't feel a great need to shake so my lazy ass will continue to type CPU rather then computer until the end of time :)/>


EDIT: Woah I'm a Scripter…when did that happen

EDIT2: Also guys I made this post a while ago so its not up to my usual quality standards but still works…kinda