Difference between revisions of "Mouse scroll (event)"

From ComputerCraft Wiki
Jump to: navigation, search
m (*cough* Apparently, the Event template doesn't allow for more than three return values?)
m (Expanded)
 
Line 7: Line 7:
 
|examples=
 
|examples=
 
{{Example
 
{{Example
|desc=Print the direction and the co-ordinates of every mouse scroll we receive a ''mouse_scroll'' event for.
+
|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
   _, dir, x, y = os.pullEvent("[[mouse_scroll_(event)|mouse_scroll]]")
+
   local event, scrollDirection, x, y = '''[[os.pullEvent]]("mouse_scroll")'''
   [[print]]("mouse_scroll: " .. [[tostring]](dir) .. ", " ..
+
   [[print]]("mouse_scroll: " .. [[tostring]](scrollDirection) .. ", " ..
 
     "X: " .. [[tostring]](x) .. ", " ..
 
     "X: " .. [[tostring]](x) .. ", " ..
 
     "Y: " .. [[tostring]](y))
 
     "Y: " .. [[tostring]](y))
Line 18: Line 18:
 
}}
 
}}
 
{{Example
 
{{Example
|desc=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.
+
|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
i = 0
+
 
  while true do
 
  while true do
     term.clear()
+
     [[term.clear]]()
     _, dir, x, y = os.pullEvent("[[mouse_scroll_(event)|mouse_scroll]]")
+
      
     if dir == -1 then
+
    local _, srollDirection, x, y = '''[[os.pullEvent]]("mouse_scroll")'''
 +
   
 +
     if scrollDirection == -1 then
 
       i = i + 1
 
       i = i + 1
     elseif dir == 1 then
+
     elseif scrollDirection == 1 then
 
       i = i - 1
 
       i = i - 1
 
     end
 
     end
     term.setCursorPos(x, y)
+
      
     term.write(i)
+
    [[term.setCursorPos]](x, y)
 +
     [[term.write]](i)
 
  end
 
  end
|output=At the coordinates of the scroll, the counter value is printed.
+
|output=At the coordinates of the scroll, the counter value <var>i</var> is printed.
 
}}
 
}}
 
}}
 
}}

Latest revision as of 17:45, 10 April 2014


Grid Modem.png  Event mouse_scroll
Fired when a mousewheel is scrolled in the terminal
Returned Object 1 The direction of the mouse-scroll (-1 = up, 1 = down).
Returned Object 2 The X-coordinate of the scroll (in screen-characters).
Returned Object 3 The Y-coordinate of the scroll (in screen-characters).

Examples

Grid paper.png  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.



Grid paper.png  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.