Common Lisp mouse position with ltk

元气小坏坏 提交于 2019-12-10 13:44:11

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/55405140/common-lisp-mouse-position-with-ltk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!