參考方法如下,具體解釋已經(jīng)注解在代碼中;
成都創(chuàng)新互聯(lián)公司專注于企業(yè)營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站重做改版、越城網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5開(kāi)發(fā)、電子商務(wù)商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為越城等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
/定義變量
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ù)下一個(gè)鉤子
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingC.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);
// 取得當(dāng)前線程編號(hào)(線程鉤子需要用到)
[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記錄下來(lái),搞個(gè)郵件發(fā)送程序發(fā)到自己的郵箱去)
return 0;//如果返回1,則結(jié)束消息,這個(gè)消息到此為止,不再傳遞。如果返回0或調(diào)用CallNextHookEx函數(shù)則消息出了這個(gè)鉤子繼續(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實(shí)例
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,你完全可以自己寫個(gè)方法獲取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ù)將鉤子加入到鉤子鏈表中,說(shuō)明一下四個(gè)參數(shù):
//idHook 鉤子類型,即確定鉤子監(jiān)聽(tīng)何種消息,上面的代碼中設(shè)為2,即監(jiān)聽(tīng)鍵盤消息并且是線程鉤子,如果是全局鉤子監(jiān)聽(tīng)鍵盤消息應(yīng)設(shè)為13,
//線程鉤子監(jiān)聽(tīng)鼠標(biāo)消息設(shè)為7,全局鉤子監(jiān)聽(tīng)鼠標(biāo)消息設(shè)為14。
//
//lpfn 鉤子子程的地址指針。如果dwThreadId參數(shù)為0 或是一個(gè)由別的進(jìn)程創(chuàng)建的線程的標(biāo)識(shí),lpfn必須指向DLL中的鉤子子程。 除此以外,lpfn可
//以指向當(dāng)前進(jìn)程的一段鉤子子程代碼。鉤子函數(shù)的入口地址,當(dāng)鉤子鉤到任何消息后便調(diào)用這個(gè)函數(shù)。
//
//hInstance應(yīng)用程序?qū)嵗木浔?。?biāo)識(shí)包含lpfn所指的子程的DLL。如果threadId 標(biāo)識(shí)當(dāng)前進(jìn)程創(chuàng)建的一個(gè)線程,而且子程代碼位于當(dāng)前
//進(jìn)程,hInstance必須為NULL。可以很簡(jiǎn)單的設(shè)定其為本應(yīng)用程序的實(shí)例句柄。
//
//threadedId 與安裝的鉤子子程相關(guān)聯(lián)的線程的標(biāo)識(shí)符。如果為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.");
}
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
假如Form1有個(gè)TextBox1,雙擊TextBox1填入123
Public Class Form1
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub TextBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.DoubleClick
System.Windows.Forms.SendKeys.Send("123")
End Sub
End Class
看你的VB.NET 版本了
用 My.Computer.Keyboard.SendKeys("123", True) 代替 System.Windows.Forms.SendKeys.Send("123") 也可以
原碼:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub TextBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.DoubleClick
My.Computer.Keyboard.SendKeys("123", True)
End Sub
End Class
既然你可以輸入英文,只要系統(tǒng)切換到中文輸入方式下,例如“谷歌拼音”,不就可以輸入中文了
用API?GetCursorPos ?
用Timer隔一段時(shí)間獲取一次鼠標(biāo)位置。
鍵盤輸入,用?GetKeyboardState
加點(diǎn)分的話就給你完整的代碼
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)
Public?Class?Form1
Private?Sub?TextBox1_KeyPress(sender?As?Object,?e?As?KeyPressEventArgs)?Handles?TextBox1.KeyPress
If?Not?IsNumeric(e.KeyChar)?Then????'判斷是否為數(shù)字
e.Handled?=?True????‘如果不是數(shù)字,取消操作,不顯示字符
End?If
End?Sub
End?Class
這樣就好啦