I'm making a simple applet in Common Lisp and I want to control it using mouse movement. I use LTK for the window. I couldn't find any function that would retrieve the mouse location. For example, Emacs Lisp has (mouse-pixel-position). I found this on rosetta code, but there's no Common Lisp entry. What can I do?
Hints from this SO answer: Mouse Position Python Tkinter
and looking at ltk's doc: http://www.peter-herth.de/ltk/ltkdoc/node16.html
I got the following example to retrieve any event fired by the mouse movement:
(ql:quickload "ltk")
(in-package :ltk-user)
(defun motion (event)
(format t "~a~&" event))
(with-ltk ()
(bind *tk* "<Motion>" #'motion))
This opens up a little window with nothing inside. Once you put the mouse in it, you get lots of events:
#S(EVENT
:X 0
:Y 85
:KEYCODE ??
:CHAR ??
:WIDTH ??
:HEIGHT ??
:ROOT-X 700
:ROOT-Y 433
:MOUSE-BUTTON ??)
…
The #S
indicates we deal with a structure, named EVENT
, so we can access its slots with (event-x event)
, event-mouse-button
, etc. See https://lispcookbook.github.io/cl-cookbook/data-structures.html#slot-access
Also you might want to join the CL community on freenode, there are some game developers there.
An event-based approach is likely to be more appropriate in most cases, but you can also query the current position directly:
(defpackage :so (:use :cl :ltk))
(in-package :so)
(with-ltk ()
(loop
(print
(multiple-value-list
(screen-mouse)))
(sleep 0.5)))
This starts a graphical toplevel and prints the current screen coordinates every 500ms, until you quit the toplevel window. The screen-mouse
function accepts an optional w
argument (a window).
© 2022 - 2024 — McMap. All rights reserved.