Example
Mouse callbacks
Example 0052: Mouse callbacks
  1. -- Example: Mouse callbacks 
  2.  
  3. -- Mousepressed: Called whenever a mouse button was pressed, 
  4. -- passing the button and the x and y coordiante it was pressed at. 
  5. function mousepressed(x, y, button) 
  6.    -- Checks which button was pressed. 
  7.    if button == love.mouse_left then 
  8.       last = "left pressed" 
  9.    elseif button == love.mouse_right then 
  10.       last = "right pressed" 
  11.    elseif button == love.mouse_middle then 
  12.       last = "middle pressed" 
  13.    elseif button == love.mouse_wheelup then 
  14.       -- Won't show up because scrollwheels are instantly "released", 
  15.       -- but the event is legitimate. 
  16.       last = "scrollwheel up pressed" 
  17.    elseif button == love.mouse_wheeldown then 
  18.       -- Won't show up because scrollwheels are instantly "released", 
  19.       -- but the event is legitimate. 
  20.       last = "scrollwheel down pressed" 
  21.    end 
  22.     
  23.    last = last .. " @ (" .. x .. "x" .. y .. ")" 
  24. end 
  25.  
  26. -- Mousereleased: Called whenever a mouse button was released, 
  27. -- passing the button and the x and y coordiante it was released at. 
  28. function mousereleased(x, y, button) 
  29.    -- Checks which button was released. 
  30.    if button == love.mouse_left then 
  31.       last = "left released" 
  32.    elseif button == love.mouse_right then 
  33.       last = "right released" 
  34.    elseif button == love.mouse_middle then 
  35.       last = "middle released" 
  36.    elseif button == love.mouse_wheelup then 
  37.       last = "scrollwheel up released" 
  38.    elseif button == love.mouse_wheeldown then 
  39.       last = "scrollwheel down released" 
  40.    end 
  41.     
  42.    last = last .. " @ (" .. x .. "x" .. y .. ")" 
  43. end 
  44.  
  45.  
  46. -- Load a font and set the text variable. 
  47. function load() 
  48.    love.graphics.setFont(love.graphics.newFont(love.default_font, 12)) 
  49.    last = "nothing" 
  50. end 
  51.  
  52. -- Output the last mouse button which was pressed/released. 
  53. function draw() 
  54.    love.graphics.draw("Last mouse click: " .. last, 100100
  55. end 
Copyright © 2006-2008 LÖVE Development Team.