「java鼠标hook」java鼠标移动事件代码
本篇文章给大家谈谈java鼠标hook,以及java鼠标移动事件代码对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、java swt win32 extension 最新jar包 Hook.MOUSE.addListener()方法怎么用
- 2、怎么用鼠标HOOK实现后台鼠标
- 3、什么是JavaHook
- 4、如何用JAVA做禁用键盘,鼠标,任务管理器等任
- 5、Java中如何在windows桌面上添加鼠标监听事件
java swt win32 extension 最新jar包 Hook.MOUSE.addListener()方法怎么用
Hook.MOUSE.addListener(111, new HookEventListener(){//111可以随便写,但是要和下面的install一直这样才能注册事件
@Override
public void acceptHookData(HookData e) {
MouseHookData KHD = ((MouseHookData)e);
KHD.getPointX();
KHD.getPointX();
if (e.getWParam() == 513) {
// 鼠标左键按下
} else if (e.getWParam() == 514) {
// 鼠标左键松开
} else if (e.getWParam() == 516) {
// 鼠标右键按下
} else if (e.getWParam() == 517) {
// 鼠标右键松开
} else if (e.getWParam() == 519) {
// 鼠标滚轮
} else if (e.getWParam() == 520) {
// 滚轮。。
}
}
});
Hook.MOUSE.install(111);
就是这样了。
怎么用鼠标HOOK实现后台鼠标
先用全局钩子把鼠标的操作记录下来,比如可以把鼠标的坐标和按下哪一个键等记录存在一个文件里。当要回放刚才的操作时,使用SendInput函数模拟鼠标的点击就行了
什么是JavaHook
javahook.dll不是一个系统文件,估计是你在安装某些软件的时候被复制到电脑中的,而你在删除这个软件的时候采用了错误的删除方式,以至于这个文件还和注册表有关联。如果现在缺少这个文件并不影响电脑的正常使用,你可以在开始菜单中选运行,键入regedit回车,在注册表编辑器的查到菜单中选查找,找到所有与javahook.dll相关的项目并删除就可以去掉这个提示了。你肯定链接没有问题吗?那你把你的主页地址告诉我,我去帮你看看可。你在中华网的网页里好好找找,应该可以找到如何设置主页的说明的。
如何用JAVA做禁用键盘,鼠标,任务管理器等任
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.internal.win32.OS;
import org.eclipse.swt.widgets.Shell;
import org.sf.feeling.swt.win32.extension.hook.Hook;
import org.sf.feeling.swt.win32.extension.hook.Keyboard_LLHook;
import org.sf.feeling.swt.win32.extension.hook.Mouse_LLHook;
import org.sf.feeling.swt.win32.extension.hook.data.Keyboard_LLHookData;
import org.sf.feeling.swt.win32.extension.hook.data.Mouse_LLHookData;
import org.sf.feeling.swt.win32.extension.hook.interceptor.InterceptorFlag;
import org.sf.feeling.swt.win32.extension.hook.interceptor.Keyboard_LLHookInterceptor;
import org.sf.feeling.swt.win32.extension.hook.interceptor.Mouse_LLHookInterceptor;
import org.sf.feeling.swt.win32.extension.registry.RegistryKey;
import org.sf.feeling.swt.win32.extension.registry.RegistryValue;
import org.sf.feeling.swt.win32.extension.registry.RootKey;
import org.sf.feeling.swt.win32.extension.registry.ValueType;
/**
* Ticket:屏蔽系统快捷键,供锁屏使用(添加禁用鼠标事件)
*
* @author XXXXX
*/
public class WinEventInterceptor {
private static Shell tagShell;
private static Keyboard_LLHookInterceptor keyboard_LLHookInterceptor;
private static Mouse_LLHookInterceptor mouse_LLHookInterceptor;
static {
keyboard_LLHookInterceptor = new Keyboard_LLHookInterceptor() {
@Override
public InterceptorFlag intercept(Keyboard_LLHookData hookData) {
int vkCode = hookData.vkCode();
boolean isCtrlPressed = OS.GetKeyState(17) 0 ? true : false;
boolean isAltPressed = OS.GetKeyState(18) 0 ? true : false;
// 屏蔽windows键
if (vkCode == 91) {
return InterceptorFlag.FALSE;
}
// 屏蔽ALT+ESC
if (isAltPressed vkCode == 27) {
return InterceptorFlag.FALSE;
}
// 屏蔽CTRL+ESC
if (isCtrlPressed vkCode == 27) {
return InterceptorFlag.FALSE;
}
// 屏蔽ALT+TAB
if (isAltPressed vkCode == 9) {
return InterceptorFlag.FALSE;
}
// 屏蔽ALT+F4
if (isAltPressed vkCode == 115) {
return InterceptorFlag.FALSE;
}
return InterceptorFlag.TRUE;
}
};
mouse_LLHookInterceptor = new Mouse_LLHookInterceptor() {
@Override
public InterceptorFlag intercept(Mouse_LLHookData hookData) {
return InterceptorFlag.FALSE;
}
};
}
/**
* 根据disable为true时,鼠标不可用;false时,鼠标可用
*
* @param disable
*/
public static void setMouseDisable(boolean disable) {
if (disable) {
Mouse_LLHook.addHookInterceptor(mouse_LLHookInterceptor);
if (!Mouse_LLHook.isInstalled())
Mouse_LLHook.installHook();
} else {
if (Mouse_LLHook.isInstalled())
Mouse_LLHook.unInstallHook();
}
}
/**
* 自定义添加禁用哪些鼠标事件
*
* @param disable
* @param mouseEvents
*/
public static void setMouseDisable(boolean disable, final MOUSE_EVENT... mouseEvents) {
if (disable) {
Mouse_LLHook.addHookInterceptor(new Mouse_LLHookInterceptor() {
@Override
public InterceptorFlag intercept(Mouse_LLHookData hookData) {
MOUSE_EVENT event = null;
if (hookData.getWParam() == 512) {
event = MOUSE_EVENT.MOVE;
} else if (hookData.getWParam() == 513 || hookData.getWParam() == 514) {
event = MOUSE_EVENT.LEFT_PRESS;
} else if (hookData.getWParam() == 516 || hookData.getWParam() == 517) {
event = MOUSE_EVENT.RIGHT_PRESS;
} else if (hookData.getWParam() == 519 || hookData.getWParam() == 520) {
event = MOUSE_EVENT.MIDDLE_PRESS;
} else if (hookData.getWParam() == 522 hookData.getMouseData() 0) {
event = MOUSE_EVENT.WHEEL_UP;
} else if (hookData.getWParam() == 522 hookData.getMouseData() 0) {
event = MOUSE_EVENT.WHEEL_DOWN;
}
for (MOUSE_EVENT mouse_event : mouseEvents) {
if (mouse_event == event) {
return InterceptorFlag.FALSE;
}
}
return InterceptorFlag.TRUE;
}
});
if (!Mouse_LLHook.isInstalled())
Mouse_LLHook.installHook();
} else {
if (Mouse_LLHook.isInstalled())
Mouse_LLHook.unInstallHook();
}
}
/**
* 禁用几个快捷键,windows键,ALT+ESC,CTRL+ESC,ALT+TAB,ALT+F4,任务管理器
*
* @param shell
* 程序主窗口
* @param disable
* 是否禁用
*/
public static void setKeyDisable(final Shell shell, boolean disable) {
if (shell == null) {
return;
}
DisposeListener disposeListener = new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
if (Hook.KEYBOARD.isInstalled(shell))
Hook.KEYBOARD.uninstall(shell);
taskmgrDisable(false);
}
};
if (disable) {
if (tagShell == shell) {
return;
}
taskmgrDisable(disable);
Keyboard_LLHook.addHookInterceptor(keyboard_LLHookInterceptor);
if (!Keyboard_LLHook.isInstalled())
Keyboard_LLHook.installHook();
shell.addDisposeListener(disposeListener);
tagShell = shell;
} else {
if (tagShell != shell) {
return;
}
taskmgrDisable(disable);
if (Keyboard_LLHook.isInstalled())
Keyboard_LLHook.unInstallHook();
shell.removeDisposeListener(disposeListener);
tagShell = null;
}
}
/**
* 禁用任务管理器
*
* @param useable
* useable:true为禁用,false为启用
*/
private static void taskmgrDisable(boolean useable) {
RootKey currentUser = RootKey.HKEY_CURRENT_USER;
RegistryKey key = new RegistryKey(currentUser,
"Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");
if (!key.exists()) {
key.create();
}
RegistryValue value = new RegistryValue();
value.setType(ValueType.REG_DWORD);
if (useable)
value.setData(1);
else
value.setData(0);
value.setName("DisableTaskmgr");
key.setValue(value);
}
enum MOUSE_EVENT {
MOVE, LEFT_PRESS, RIGHT_PRESS, MIDDLE_PRESS, WHEEL_UP, WHEEL_DOWN
}
}
其中
setKeyDisable(final Shell shell, boolean disable)
这个方法是因为程序的特殊要求,可以不需要shell。
屏蔽任务管理器是用了一个取巧的办法,在注册表中设置字段,让其不可用。屏蔽快捷键+注册表不可用时,程序就实现锁屏无法切换了。
鼠标事件是后添加的。
Java中如何在windows桌面上添加鼠标监听事件
去下载 JInvoke , 这是一个例子:如果网上找不到 JInvoke.jar,我传了一个到网站了, 打开后在上级目录[..]的myfiles目录里能找到.ji.zip即是
import static com.jinvoke.win32.WinConstants.*;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import com.jinvoke.Callback;
import com.jinvoke.JInvoke;
import com.jinvoke.NativeImport;
import com.jinvoke.Util;
import com.jinvoke.win32.User32;
import com.jinvoke.win32.structs.Msg;
public class MouseHook extends JPanel{
static {
JInvoke.initialize();
}
@NativeImport(library = "user32")
public native static int SetWindowsHookEx (int idHook, Callback hookProc, int hModule, int dwThreadId);
@NativeImport(library = "user32")
public native static int UnhookWindowsHookEx (int idHook);
public static final int WH_MOUSE_LL = 14;
static JFrame frame;
static TextArea mouseEventArea = new TextArea();
static JButton setHookBtn;
static JButton removeHookBtn;
public MouseHook() {
super(new BorderLayout());
mouseEventArea.setText("1) Click the \"Set Mouse Hook\" button.\n" +
"2) Start clicking anywhere on the desktop. Mouse clicks will be captured here.\n" +
"3) Stop the mouse hook by clicking the \"Remove Mouse Hook\" button.\n\n");
JScrollPane MouseEventPane = new JScrollPane(mouseEventArea);
add(MouseEventPane, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
setHookBtn = new JButton("Set Mouse Hook");
setHookBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
setMouseHook();
}} );
removeHookBtn = new JButton("Remove Mouse Hook");
removeHookBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
unsetMouseHook();
}} );
removeHookBtn.setEnabled(false);
buttonPanel.add(setHookBtn);
buttonPanel.add(removeHookBtn);
add(buttonPanel, BorderLayout.SOUTH);
}
private void setMouseHook() {
setHookBtn.setEnabled(false);
removeHookBtn.setEnabled(true);
// This hook is called in the context of the thread that installed it.
// The call is made by sending a message to the thread that installed the hook.
// Therefore, the thread that installed the hook must have a message loop.
//
// We crate a new thread as we don't want the AWT Event thread to be stuck running a message pump
// nor do we want the main thread to be stuck in running a message pump
Thread hookThread = new Thread(new Runnable(){
public void run() {
if (MouseProc.hookHandle == 0) {
int hInstance = User32.GetWindowLong(Util.getWindowHandle(frame), GWL_HINSTANCE);
MouseProc.hookHandle = SetWindowsHookEx(WH_MOUSE_LL,
new Callback(MouseProc.class, "lowLevelMouseProc"),
hInstance,
0);
// Standard message dispatch loop (message pump)
Msg msg = new Msg();
while (User32.GetMessage(msg, 0, 0, 0)) {
User32.TranslateMessage(msg);
User32.DispatchMessage(msg);
}
} else {
mouseEventArea.append("The Hook is already installed.\n");
}
}});
hookThread.start();
}
private void unsetMouseHook() {
setHookBtn.setEnabled(true);
removeHookBtn.setEnabled(false);
UnhookWindowsHookEx(MouseProc.hookHandle);
MouseProc.hookHandle = 0;
}
private static void createAndShowGUI() {
//Create and set up the window.
frame = new JFrame("Mouse Hook");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MouseHook MouseEventsWindow = new MouseHook();
MouseEventsWindow.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
//Add content to the window.
frame.add(MouseEventsWindow, BorderLayout.CENTER);
//Display the window.
frame.pack();
frame.setBounds(300, 200, 750, 600);
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
class MouseProc {
static int hookHandle;
@NativeImport(library = "user32")
public native static int CallNextHookEx (int idHook, int nCode, int wParam, int lParam);
static {
JInvoke.initialize();
}
public static int lowLevelMouseProc(int nCode, int wParam, int lParam ) {
if (nCode 0)
return CallNextHookEx(hookHandle, nCode, wParam, lParam);
if (nCode == HC_ACTION) {
MouseHookStruct mInfo = Util.ptrToStruct(lParam, MouseHookStruct.class);
String message = "Mouse pt: (" + mInfo.pt.x + ", " + mInfo.pt.y + ") ";
switch (wParam) {
case WM_LBUTTONDOWN:
message += "Left button down";
break;
case WM_LBUTTONUP:
message += "Left button up";
break;
case WM_MOUSEMOVE:
message += "Mouse moved";
break;
case WM_MOUSEWHEEL:
message += "Mouse wheel rotated";
break;
case WM_RBUTTONDOWN:
message += "Right button down";
break;
case WM_RBUTTONUP:
message += "Right button down";
break;
}
System.out.println(message);
// MouseHook.mouseEventArea.append(message+"\n");
}
return CallNextHookEx(hookHandle, nCode, wParam, lParam);
}
}
=============================================
import com.jinvoke.NativeStruct;
import com.jinvoke.win32.structs.Point;
@NativeStruct
public class MouseHookStruct {//MSLLHOOKSTRUCT
public Point pt = new Point();
public int mouseData;
public int flags;
public int time;
public int dwExtraInfo;
}
java鼠标hook的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java鼠标移动事件代码、java鼠标hook的信息别忘了在本站进行查找喔。