Difference between revisions of "Mouse scroll (event)"
From ComputerCraft Wiki
TheCoryKid (Talk | contribs) (The description and code example were both wrong. Fixed the return values, changed the example to reflect this, and added a second example.) |
MKlegoman357 (Talk | contribs) m (Expanded) |
||
(2 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
− | |||
− | |||
{{Event | {{Event | ||
|name=mouse_scroll | |name=mouse_scroll | ||
|desc=Fired when a mousewheel is scrolled in the terminal | |desc=Fired when a mousewheel is scrolled in the terminal | ||
− | |return1 | + | |return1=The direction of the mouse-scroll (<var>-1</var> = up, <var>1</var> = down). |
− | + | |return2=The X-coordinate of the scroll (in screen-characters). | |
− | | | + | |return3=The Y-coordinate of the scroll (in screen-characters). |
− | | | + | |
|examples= | |examples= | ||
{{Example | {{Example | ||
− | |desc= | + | |desc=Prints the direction and the co-ordinates of every mouse scroll we receive a ''mouse_scroll'' event for. |
|code= | |code= | ||
while true do | while true do | ||
− | + | local event, scrollDirection, x, y = '''[[os.pullEvent]]("mouse_scroll")''' | |
− | [[print]]("mouse_scroll: " .. [[tostring]]( | + | [[print]]("mouse_scroll: " .. [[tostring]](scrollDirection) .. ", " .. |
"X: " .. [[tostring]](x) .. ", " .. | "X: " .. [[tostring]](x) .. ", " .. | ||
"Y: " .. [[tostring]](y)) | "Y: " .. [[tostring]](y)) | ||
Line 21: | Line 18: | ||
}} | }} | ||
{{Example | {{Example | ||
− | |desc=A variable | + | |desc=A variable <var>i</var> keeps track of the relative value of the scroll: every time a mouse-scroll occurs, the code checks the direction, incrementing the variable by one for every scroll up and decrementing the variable by one for every scroll down. |
− | |code= | + | |code= local i = 0 |
− | + | ||
while true do | while true do | ||
− | + | [[term.clear]]() | |
− | + | ||
− | + | local _, srollDirection, x, y = '''[[os.pullEvent]]("mouse_scroll")''' | |
− | + | ||
− | + | if scrollDirection == -1 then | |
− | + | i = i + 1 | |
− | + | elseif scrollDirection == 1 then | |
− | + | i = i - 1 | |
− | + | end | |
− | |output=At the coordinates of the scroll, the counter value is printed. | + | |
+ | [[term.setCursorPos]](x, y) | ||
+ | [[term.write]](i) | ||
+ | end | ||
+ | |output=At the coordinates of the scroll, the counter value <var>i</var> is printed. | ||
}} | }} | ||
}} | }} |
Latest revision as of 17:45, 10 April 2014
Examples
Example | |
Prints the direction and the co-ordinates of every mouse scroll we receive a mouse_scroll event for. | |
Code |
while true do local event, scrollDirection, x, y = os.pullEvent("mouse_scroll") print("mouse_scroll: " .. tostring(scrollDirection) .. ", " .. "X: " .. tostring(x) .. ", " .. "Y: " .. tostring(y)) end |
Output | The direction the mouse-wheel was scrolled in, followed by the X and Y position of the event. |
Example | |
A variable i keeps track of the relative value of the scroll: every time a mouse-scroll occurs, the code checks the direction, incrementing the variable by one for every scroll up and decrementing the variable by one for every scroll down. | |
Code |
local i = 0 while true do term.clear() local _, srollDirection, x, y = os.pullEvent("mouse_scroll") if scrollDirection == -1 then i = i + 1 elseif scrollDirection == 1 then i = i - 1 end term.setCursorPos(x, y) term.write(i) end |
Output | At the coordinates of the scroll, the counter value i is printed. |