Search code examples
common-lispltk

Common Lisp mouse position with ltk


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?


Solution

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