マウス入力
マウスの入力を受け取る
マウスのボタンが押された入力を受け取る場合、love.mousepressedコールバック関数を使用します。
inX = 0
inY = 0
function love.draw()
love.graphics.print("x:" .. inX .. ", y:" .. inY, 20, 20)
end
function love.mousepressed(x, y, button, istouch)
inX = x
inY = y
end

マウスをクリックすると、クリックした座標の情報が表示されます。
マウスの状態を詳しく受け取る
Love2Dでは「マウスのボタンが押されたとき」「マウスのボタンが離されたとき」「マウスが移動したとき」「ホイールが移動したとき」の状態を取得することができます。
text = ""
function love.draw()
love.graphics.print(text, 20, 20)
end
function love.mousemoved(x, y, dx, dy, istouch)
text = "mousemoved: (" .. x .. ", " .. y .. ")"
end
function love.mousepressed(x, y, button, istouch, presses)
text = "mousepressed: (" .. x .. ", " .. y .. ")"
end
function love.mousereleased(x, y, button, istouch, presses)
text = "mousereleased: (" .. x .. ", " .. y .. ")"
end
function love.wheelmoved(x, y)
text = "wheelmoved: (" .. x .. ", " .. y .. ")"
end

マウスの状態が文字列で表示されています。
マウスのボタンを判別する
アプリケーションやゲームでは左クリックと右クリックの判別する必要があります。中央ボタン(ホイール)も判別する必要があるかもしれません。クリックされたボタンの種類の情報はlove.mousepressed関数のbuttonで判定できます。
inButton = 0
function love.draw()
if inButton == 1 then
love.graphics.print("Left Click", 20, 20)
elseif inButton == 2 then
love.graphics.print("Right Click", 20, 20)
elseif inButton == 3 then
love.graphics.print("Center Click", 20, 20)
end
end
function love.mousepressed(x, y, button, istouch)
inButton = button
end

マウスをクリックすると、クリックしたボタンの種類が表示されます。