问题
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).
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.
And finally, there's CommonQt, but while there seems to be QtCursor::setPos in Qt, I couldn't find the CL version.
回答1:
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))
The #+win32
means that this is only evaluated on Windows.
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))
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"))
Now calling (set-cursor-pos 100 100)
should move the mouse near the top left corner.
回答2:
There are two problems here:
- How to move the mouse in windows
- How to call that function from CL.
It seems you have figured out a suitable win32 function exists so the challenge is to load the relevant library, declare the functions name and type, and then call it. I can’t really help you with that unfortunately.
Some other solutions you might try:
- Write and compile a trivial C library to call the function you want and see if you can call that from CL (maybe this is easier?)
- Write and compile a trivial C library and see if you can work out how to call it from CL
- Write/find some trivial program in another language to move the mouse based on arguments/stdin and run that from CL
来源:https://stackoverflow.com/questions/55736410/how-can-the-mouse-be-moved-programatically-in-common-lisp