如何在Common Lisp中以编程方式移动鼠标? [英] How can the mouse be moved programatically in Common Lisp?
问题描述
该代码应在Windows 10上运行.我尝试在Reddit上询问,但是这些想法仅适用于Unix/Linux.也有CFFI,但我不知道如何使用它解决此问题(我找到的文档只是与该问题无关的详尽示例.
The code should run on Windows 10. I tried asking on Reddit, but the ideas are Unix/Linux only. There's also CFFI, but I didn't understand how to use it for this problem (the main usability part of the documentation I found is just an elaborate example not related to this problem).
我还浏览了Python的SetCursorPos,发现它调用了ctypes.windll.user32.SetCursorPos(x,y),但我不知道CL中的样子.
I also looked through the SetCursorPos of Python, and found that it calls ctypes.windll.user32.SetCursorPos(x, y), but I have no clue what that would look like in CL.
最后,还有CommonQt,但是虽然Qt中似乎有QtCursor :: setPos,但我找不到CL版本.
And finally, there's CommonQt, but while there seems to be QtCursor::setPos in Qt, I couldn't find the CL version.
推荐答案
The function called by the Python example seems to be documented here. It is part of a shared library user32.dll
, which you can load with CFFI,
(ql:quickload :cffi)
#+win32
(progn
(cffi:define-foreign-library user32
(:windows "user32.dll"))
(cffi:use-foreign-library user32))
#+win32
表示仅在Windows上评估.
The #+win32
means that this is only evaluated on Windows.
然后可以使用CFFI:DEFCFUN
声明外部SetCursorPos
函数.根据文档,它接受两个int
并返回一个BOOL
. CFFI具有:BOOL
类型,但是Windows BOOL
似乎实际上是int
.您可能可以使用cffi-grovel从某些Windows标头中自动找到typedef,但我只在这里直接使用:INT
.
Then you can declare the foreign SetCursorPos
-function with CFFI:DEFCFUN
. According to the documentation, it takes in two int
s and returns a BOOL
. CFFI has a :BOOL
-type, however the Windows BOOL
seems to actually be an int
. You could probably use cffi-grovel to automatically find the typedef from some Windows header, but I'll just use :INT
directly here.
#+win32
(cffi:defcfun ("SetCursorPos" %set-cursor-pos) (:boolean :int)
(x :int)
(y :int))
我在名称中放置了%
,以指示这是一个内部函数,不应直接调用它(因为它仅在Windows上可用).然后,您应该编写一个在不同平台上都可以使用的包装器(实际上,这里没有在其他平台上实现它).
I put a %
in the name to indicate this is an internal function that should not be called directly (because it is only available on Windows). You should then write a wrapper that works on different platforms (actually implementing it on other platforms is left out here).
(defun set-cursor-pos (x y)
(check-type x integer)
(check-type y integer)
#+win32 (%set-cursor-pos x y)
#-win32 (error "Not supported on this platform"))
现在调用(set-cursor-pos 100 100)
会将鼠标移到左上角附近.
Now calling (set-cursor-pos 100 100)
should move the mouse near the top left corner.
这篇关于如何在Common Lisp中以编程方式移动鼠标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!