用API?GetCursorPos ?
創(chuàng)新互聯(lián)公司主營廬陽網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶App定制開發(fā),廬陽h5微信平臺小程序開發(fā)搭建,廬陽網(wǎng)站營銷推廣歡迎廬陽等地區(qū)企業(yè)咨詢
用Timer隔一段時間獲取一次鼠標(biāo)位置。
鍵盤輸入,用?GetKeyboardState
加點分的話就給你完整的代碼
Public?Class?NativeMethods
'獲取鼠標(biāo)位置
Declare?Function?GetCursorPos?Lib?"user32.dll"?_
(ByRef?lpPoint?As?System.Drawing.Point)?as?boolean
Private?Shared?keyState()?As?Byte
DllImport("user32.dll")?_
Private?Shared?Function?GetKeyboardState(ByVal?keyState()?As?Byte)?As?Boolean
End?Function
Private?Shared?Sub?Update()
keyState?=?New?Byte(256)?{}
Dim?result?As?Boolean?=?GetKeyboardState(keyState)
'?Check?for?error:
If?result?=?False?Then
Debug.WriteLine("GetKeyBoardState?error:?"??Marshal.GetLastWin32Error)
Throw?New?Exception("GetKeyBoardState?error:?"??Marshal.GetLastWin32Error)
End?If
End?Sub
Public?Enum?LightState
Off
[On]
End?Enum
'?Example?-?the?keyboard?lights...
Public?Shared?ReadOnly?Property?CapsLockState()?As?LightState
Get
Update()
Dim?isOn?As?Boolean?=?(keyState(Keys.CapsLock)?=?1)
Return?IIf(isOn,?LightState.On,?LightState.Off)
End?Get
End?Property
Public?Shared?ReadOnly?Property?NumLockState()?As?LightState
Get
Update()
Dim?isOn?As?Boolean?=?(keyState(Keys.NumLock)?=?1)
Return?IIf(isOn,?LightState.On,?LightState.Off)
End?Get
End?Property
Public?Shared?ReadOnly?Property?ScrollLockState()?As?LightState
Get
Update()
Dim?isOn?As?Boolean?=?(keyState(Keys.Scroll)?=?1)
Return?IIf(isOn,?LightState.On,?LightState.Off)
End?Get
End?Property
End?Class
pinvoke.net: GetCursorPos (user32)
pinvoke.net: GetKeyboardState (user32)
參考方法如下,具體解釋已經(jīng)注解在代碼中;
/定義變量
public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
static int hKeyboardHook = 0;
HookProc KeyboardHookProcedure;
/*************************
* 聲明API函數(shù)
* ***********************/
// 安裝鉤子 (using System.Runtime.InteropServices;)
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingC.StdCall)]
public static extern int SetWindowsHookEx(int idHook,HookProc lpfn, IntPtr hInstance, int threadId);
// 卸載鉤子
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingC.StdCall)]
public static extern bool UnhookWindowsHookEx(int idHook);
// 繼續(xù)下一個鉤子
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingC.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);
// 取得當(dāng)前線程編號(線程鉤子需要用到)
[DllImport("kernel32.dll")]
static extern int GetCurrentThreadId();
//鉤子子程:就是鉤子所要做的事情
private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
{
if (nCode = 0)
{
/****************
//線程鍵盤鉤子判斷是否按下鍵
Keys keyData = (Keys)wParam;
if(lParam.ToInt32() 0)
{
// 鍵盤按下
}
if(lParam.ToInt32() 0)
{
// 鍵盤抬起
}
****************/
/****************
//全局鍵盤鉤子判斷是否按下鍵
wParam = = 0x100 // 鍵盤按下
wParam = = 0x101 // 鍵盤抬起
****************/
KeyMSG m = (KeyMSG) Marshal.PtrToStructure(lParam, typeof(KeyMSG));//鍵盤
// 在這里添加你想要做是事情(比如把鍵盤nCode記錄下來,搞個郵件發(fā)送程序發(fā)到自己的郵箱去)
return 0;//如果返回1,則結(jié)束消息,這個消息到此為止,不再傳遞。如果返回0或調(diào)用CallNextHookEx函數(shù)則消息出了這個鉤子繼續(xù)往下傳遞,也就是傳給消息真正的接受者
}
return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
}
//鍵盤結(jié)構(gòu)
public struct KeyMSG
{
public int vkCode; //鍵值
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
}
// 安裝鉤子
public void HookStart()
{
if(hKeyboardHook == 0)
{
// 創(chuàng)建HookProc實例
KeyboardHookProcedure = new HookProc(KeyboardHookProc);
// 設(shè)置線程鉤子
hKeyboardHook = SetWindowsHookEx( 13,KeyboardHookProcedure,Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),0);
//************************************
//鍵盤線程鉤子
//SetWindowsHookEx( 2,KeyboardHookProcedure, IntPtr.Zero, GetCurrentThreadId()); //GetCurrentThreadId()為要監(jiān)視的線程ID,你完全可以自己寫個方法獲取QQ的線程哦
//鍵盤全局鉤子,需要引用空間(using System.Reflection;)
//SetWindowsHookEx( 13,KeyboardHookProcedure,Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),0);
//
//關(guān)于SetWindowsHookEx (int idHook, HookProc lpfn, IntPtr hInstance, int threadId)函數(shù)將鉤子加入到鉤子鏈表中,說明一下四個參數(shù):
//idHook 鉤子類型,即確定鉤子監(jiān)聽何種消息,上面的代碼中設(shè)為2,即監(jiān)聽鍵盤消息并且是線程鉤子,如果是全局鉤子監(jiān)聽鍵盤消息應(yīng)設(shè)為13,
//線程鉤子監(jiān)聽鼠標(biāo)消息設(shè)為7,全局鉤子監(jiān)聽鼠標(biāo)消息設(shè)為14。
//
//lpfn 鉤子子程的地址指針。如果dwThreadId參數(shù)為0 或是一個由別的進(jìn)程創(chuàng)建的線程的標(biāo)識,lpfn必須指向DLL中的鉤子子程。 除此以外,lpfn可
//以指向當(dāng)前進(jìn)程的一段鉤子子程代碼。鉤子函數(shù)的入口地址,當(dāng)鉤子鉤到任何消息后便調(diào)用這個函數(shù)。
//
//hInstance應(yīng)用程序?qū)嵗木浔?。?biāo)識包含lpfn所指的子程的DLL。如果threadId 標(biāo)識當(dāng)前進(jìn)程創(chuàng)建的一個線程,而且子程代碼位于當(dāng)前
//進(jìn)程,hInstance必須為NULL??梢院芎唵蔚脑O(shè)定其為本應(yīng)用程序的實例句柄。
//
//threadedId 與安裝的鉤子子程相關(guān)聯(lián)的線程的標(biāo)識符。如果為0,鉤子子程與所有的線程關(guān)聯(lián),即為全局鉤子。
//************************************
// 如果設(shè)置鉤子失敗
if(hKeyboardHook == 0 )
{
HookStop();
throw new Exception("SetWindowsHookEx failed.");
}
}
}
// 卸載鉤子
public void HookStop()
{
bool retKeyboard = true;
if(hKeyboardHook != 0)
{
retKeyboard = UnhookWindowsHookEx(hKeyboardHook);
hKeyboardHook = 0;
}
if (!( retKeyboard))
throw new Exception("UnhookWindowsHookEx failed.");
}
程序調(diào)試通過,附上截圖。代碼如下,歡迎交流。
采用的方法是輾除法。
’-------------------------------------
Public?Class?Form1
Private?Sub?test()
Dim?a?As?Integer
Dim?b?As?Integer
Try
a?=?CInt(TextBox1.Text)
b?=?CInt(TextBox2.Text)
Catch?ex?As?Exception
MsgBox(ex.Message)
Exit?Sub
End?Try
Dim?r?As?Integer?=?findTheNumber(a,?b)
TextBox3.Text?=?r.ToString
End?Sub
Private?Function?findTheNumber(ByVal?a?As?Integer,?ByVal?b?As?Integer)?As?Integer
If?a??b?Then
Dim?temp?As?Integer?=?a
a?=?b
b?=?temp
End?If
'輾除法
Dim?temp_a?As?Integer
While?b??0
temp_a?=?a?Mod?b
a?=?b
b?=?temp_a
End?While
Return?a
End?Function
Private?Sub?Button1_Click(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?Button1.Click
test()
End?Sub
End?Class
Private?Sub?Form1_KeyPress(ByVal?sender?As?Object,?ByVal?e?As?System.Windows.Forms.KeyPressEventArgs)?Handles?Me.KeyPress
If?Asc(0)?Then
textbox1.text="0"
end?if
end?sub