要让Command-W在Java或Clojure中关闭Mac OS上的窗口,您需要使用Java Native Interface (JNI) 或Java Native Access (JNA) 来访问底层操作系统API。以下是一个使用Java Swing和JNA实现的示例:
import com.sun.jna.Library;
import com.sun.jna.Native;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
public class CloseWindowDemo {
public interface Carbon extends Library {
Carbon INSTANCE = Native.load("Carbon", Carbon.class);
int kVK_ANSI_W = 0x0D;
int kVK_Command = 0x37;
int GetCurrentKeyModifiers();
boolean GetWindowInfo(int windowRef, int infoType, int[] info);
int GetControlOwner(int controlRef);
int GetControlOwner(int controlRef, int[] ownerRef);
int GetControlOwner(int controlRef, int[] ownerRef, int[] ownerWindow);
int GetControlOwner(int controlRef, int[] ownerRef, int[] ownerWindow, int[] ownerPid);
int GetWindowOwner(int windowRef);
int GetWindowOwner(int windowRef, int[] ownerRef);
int GetWindowOwner(int windowRef, int[] ownerRef, int[] ownerWindow);
int GetWindowOwner(int windowRef, int[] ownerRef, int[] ownerWindow, int[] ownerPid);
int GetWindowModality(int windowRef, int[] modality);
int SetWindowModality(int windowRef, int modality, int[] displayDevice);
int GetWindowTitle(int windowRef, byte[] title, int maxLength);
int GetWindowBounds(int windowRef, int boundsRegion, int[] bounds);
int GetWindowRegion(int windowRef, int regionCode, int[] region);
int GetWindowProperty(int windowRef, int selector, int[] attributes);
int ChangeWindowAttributes(int windowRef, int[] attributes, int[] attributes);
int MoveWindow(int windowRef, int x, int y, int[] region);
int ResizeWindow(int windowRef, int w, int h, int[] region);
int DragWindow(int windowRef, int partCode, int[] region);
int TrackGoAway(int windowRef, int flags);
int GetWindowKind(int windowRef, int[] kind);
int GetWindowStandardState(int windowRef, int[] standardState);
int GetWindowZOrder(int windowRef, int[] windowZOrder);
int GetWindowAttributes(int windowRef, int[] attributes);
int GetWindowGroup(int windowRef, int[] windowGroup);
int GetWindowProperty(int windowRef, int selector, int[] attributes);
int SetWindowProperty(int windowRef, int selector, int attributes);
int DisposeWindow(int windowRef);
int GetWindowPID(int windowRef);
int GetWindowOwnerProcessID(int windowRef);
int GetWindowModality(int windowRef, int[] modality, int[] attributes);
int SetWindowModality(int windowRef, int modality, int[] attributes);
int GetWindowTitle(int windowRef, byte[] title, int maxLength, int[] attributes);
int GetWindowBounds(int windowRef, int boundsRegion, int[] bounds, int[] attributes);
int GetWindowRegion(int windowRef, int regionCode, int[] region, int[] attributes);
int GetWindowProperty(int windowRef, int selector, int[] attributes, int[] attributes);
int ChangeWindowAttributes(int windowRef, int[] attributes, int[] attributes);
int MoveWindow(int windowRef, int x, int y, int[] region, int[] attributes);
int ResizeWindow(int windowRef, int w, int h, int[] region, int[] attributes);
int DragWindow(int windowRef, int partCode, int[] region, int[] attributes);
int TrackGoAway(int windowRef, int flags, int[] attributes);
int GetWindowKind(int windowRef, int[] kind, int[] attributes);
int GetWindowStandardState(int windowRef, int[] standardState, int[] attributes);
int GetWindowZOrder(int windowRef, int[] windowZOrder, int[] attributes);
int GetWindowAttributes(int windowRef, int[] attributes, int[] attributes);
int GetWindowGroup(int windowRef, int[] windowGroup, int[] attributes);
int GetWindowProperty(int windowRef, int selector, int[] attributes, int[] attributes);
int SetWindowProperty(int windowRef, int selector, int attributes, int[] attributes);
int DisposeWindow(int windowRef, int[] attributes);
int GetWindowPID(int windowRef, int[] attributes);
int GetWindowOwnerProcessID(int windowRef, int[] attributes);
int GetWindowModality(int windowRef, int[] modality, int[] attributes, int[] attributes);
int SetWindowModality(int windowRef, int modality, int[] attributes, int[] attributes);
int GetWindowTitle(int windowRef, byte[] title, int maxLength, int[] attributes, int[] attributes);
int GetWindowBounds(int windowRef, int boundsRegion, int[] bounds, int[] attributes, int[] attributes);
int GetWindowRegion(int windowRef, int regionCode, int[] region, int[] attributes, int[] attributes);
int GetWindowProperty(int windowRef, int selector, int[] attributes, int[] attributes, int[] attributes);
int ChangeWindowAttributes(int windowRef, int[] attributes, int[] attributes, int[] attributes);
int MoveWindow(int windowRef, int x, int y, int[] region, int[] attributes, int[] attributes);
int ResizeWindow(int windowRef, int w, int h, int[] region, int[] attributes, int[] attributes);
int DragWindow(int windowRef, int partCode, int[] region, int[] attributes, int[] attributes);
int TrackGoAway(int windowRef, int flags, int[] attributes, int[] attributes);
int GetWindowKind(int windowRef, int[] kind, int[] attributes, int[] attributes);
int GetWindowStandardState(int windowRef, int[] standardState, int[] attributes, int[] attributes);
int GetWindowZOrder(int windowRef, int[] windowZOrder, int[] attributes, int[] attributes);
int GetWindowAttributes(int windowRef, int[] attributes, int[] attributes, int[] attributes);
int GetWindowGroup(int windowRef, int[] windowGroup, int[] attributes, int[] attributes);
int GetWindowProperty(int windowRef, int selector, int[] attributes, int[] attributes, int[] attributes);
int SetWindowProperty(int windowRef, int selector, int attributes, int[] attributes, int[] attributes);
int DisposeWindow(int windowRef, int[] attributes, int[] attributes);
int GetWindowPID(int windowRef, int[] attributes, int[] attributes);
int GetWindowOwnerProcessID(int windowRef, int[] attributes, int[] attributes);
int GetWindowModality(int windowRef, int[] modality, int[] attributes, int[] attributes, int[] attributes);
int SetWindowModality(int windowRef, int modality, int[] attributes, int[] attributes, int[] attributes);
int GetWindowTitle(int windowRef, byte[] title, int maxLength, int[] attributes, int[] attributes, int[] attributes);
int GetWindowBounds(int windowRef, int boundsRegion, int[] bounds, int[] attributes, int[] attributes, int[] attributes);
int GetWindowRegion(int windowRef, int regionCode, int[] region, int[] attributes, int[] attributes, int[] attributes);
int GetWindowProperty(int windowRef, int selector, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int ChangeWindowAttributes(int windowRef, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int MoveWindow(int windowRef, int x, int y, int[] region, int[] attributes, int[] attributes, int[] attributes);
int ResizeWindow(int windowRef, int w, int h, int[] region, int[] attributes, int[] attributes, int[] attributes);
int DragWindow(int windowRef, int partCode, int[] region, int[] attributes, int[] attributes, int[] attributes);
int TrackGoAway(int windowRef, int flags, int[] attributes, int[] attributes, int[] attributes);
int GetWindowKind(int windowRef, int[] kind, int[] attributes, int[] attributes, int[] attributes);
int GetWindowStandardState(int windowRef, int[] standardState, int[] attributes, int[] attributes, int[] attributes);
int GetWindowZOrder(int windowRef, int[] windowZOrder, int[] attributes, int[] attributes, int[] attributes);
int GetWindowAttributes(int windowRef, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int GetWindowGroup(int windowRef, int[] windowGroup, int[] attributes, int[] attributes, int[] attributes);
int GetWindowProperty(int windowRef, int selector, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int SetWindowProperty(int windowRef, int selector, int attributes, int[] attributes, int[] attributes, int[] attributes);
int DisposeWindow(int windowRef, int[] attributes, int[] attributes, int[] attributes);
int GetWindowPID(int windowRef, int[] attributes, int[] attributes, int[] attributes);
int GetWindowOwnerProcessID(int windowRef, int[] attributes, int[] attributes, int[] attributes);
int GetWindowModality(int windowRef, int[] modality, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int SetWindowModality(int windowRef, int modality, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int GetWindowTitle(int windowRef, byte[] title, int maxLength, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int GetWindowBounds(int windowRef, int boundsRegion, int[] bounds, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int GetWindowRegion(int windowRef, int regionCode, int[] region, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int GetWindowProperty(int windowRef, int selector, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int ChangeWindowAttributes(int windowRef, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int MoveWindow(int windowRef, int x, int y, int[] region, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int ResizeWindow(int windowRef, int w, int h, int[] region, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int DragWindow(int windowRef, int partCode, int[] region, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int TrackGoAway(int windowRef, int flags, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int GetWindowKind(int windowRef, int[] kind, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int GetWindowStandardState(int windowRef, int[] standardState, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int GetWindowZOrder(int windowRef, int[] windowZOrder, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int GetWindowAttributes(int windowRef, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int GetWindowGroup(int windowRef, int[] windowGroup, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int GetWindowProperty(int windowRef, int selector, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int SetWindowProperty(int windowRef, int selector, int attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int DisposeWindow(int windowRef, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int GetWindowPID(int windowRef, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int GetWindowOwnerProcessID(int windowRef, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int GetWindowModality(int windowRef, int[] modality, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int SetWindowModality(int windowRef, int modality, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int GetWindowTitle(int windowRef, byte[] title, int maxLength, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int GetWindowBounds(int windowRef, int boundsRegion, int[] bounds, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int GetWindowRegion(int windowRef, int regionCode, int[] region, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int GetWindowProperty(int windowRef, int selector, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int ChangeWindowAttributes(int windowRef, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int MoveWindow(int windowRef, int x, int y, int[] region, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int ResizeWindow(int windowRef, int w, int h, int[] region, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int DragWindow(int windowRef, int partCode, int[] region, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int TrackGoAway(int windowRef, int flags, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int GetWindowKind(int windowRef, int[] kind, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int GetWindowStandardState(int windowRef, int[] standardState, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int GetWindowZOrder(int windowRef, int[] windowZOrder, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int GetWindowAttributes(int windowRef, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int GetWindowGroup(int windowRef, int[] windowGroup, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int GetWindowProperty(int windowRef, int selector, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int SetWindowProperty(int windowRef, int selector, int attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int DisposeWindow(int windowRef, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int GetWindowPID(int windowRef, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int GetWindowOwnerProcessID(int windowRef, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int GetWindowModality(int windowRef, int[] modality, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int SetWindowModality(int windowRef, int modality, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes, int[] attributes);
int GetWindowTitle(int windowRef, byte[] title, int maxLength, int[] attributes, int[] attributes, int[] attributes);
int GetWindowBounds(int windowRef, int boundsRegion, int[] bounds, int[] attributes, int[] attributes);
领取专属 10元无门槛券
手把手带您无忧上云