Common Lisp mouse position with ltk
Asked Answered
C

2

5

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?

Cosmos answered 28/3, 2019 at 19:5 Comment(1)
added ! rosettacode.org/wiki/Mouse_position#Common_LispCartwell
C
8

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.

Cartwell answered 28/3, 2019 at 19:21 Comment(0)
S
1

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).

Superlative answered 29/3, 2019 at 11:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.