How can the mouse be moved programatically in Common Lisp?

送分小仙女□ 提交于 2019-12-24 12:25:55

问题


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 ints 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:

  1. How to move the mouse in windows
  2. 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:

  1. 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?)
  2. Write and compile a trivial C library and see if you can work out how to call it from CL
  3. 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

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