參考方法如下,具體解釋已經(jīng)注解在代碼中;
創(chuàng)新互聯(lián)專注于光山網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供光山營銷型網(wǎng)站建設(shè),光山網(wǎng)站制作、光山網(wǎng)頁設(shè)計、光山網(wǎng)站官網(wǎng)定制、微信小程序開發(fā)服務(wù),打造光山網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供光山網(wǎng)站排名全網(wǎng)營銷落地服務(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ù)下一個鉤子
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingC.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);
// 取得當前線程編號(線程鉤子需要用到)
[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)聽鼠標消息設(shè)為7,全局鉤子監(jiān)聽鼠標消息設(shè)為14。
//
//lpfn 鉤子子程的地址指針。如果dwThreadId參數(shù)為0 或是一個由別的進程創(chuàng)建的線程的標識,lpfn必須指向DLL中的鉤子子程。 除此以外,lpfn可
//以指向當前進程的一段鉤子子程代碼。鉤子函數(shù)的入口地址,當鉤子鉤到任何消息后便調(diào)用這個函數(shù)。
//
//hInstance應(yīng)用程序?qū)嵗木浔俗R包含lpfn所指的子程的DLL。如果threadId 標識當前進程創(chuàng)建的一個線程,而且子程代碼位于當前
//進程,hInstance必須為NULL??梢院芎唵蔚脑O(shè)定其為本應(yīng)用程序的實例句柄。
//
//threadedId 與安裝的鉤子子程相關(guān)聯(lián)的線程的標識符。如果為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.");
}
Public Sub TextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
Dim SplitStr As String = ","
Dim SelectionStart As Integer = sender.SelectionStart
Dim TextLength As Integer = sender.Text.Length
'------------------------------------------------------------------
Select Case Asc(e.KeyChar)
Case Is = 8 '"回刪"
Dim str As String = sender.text
Dim Array = Split(sender.text, ",", -1)
If sender.SelectionStart = str.Length Then
If str.Contains(",") Then
Dim text = ""
For x = 0 To UBound(Array) - 1
If text = "" Then
text += Array(x)
Else
text += "," + Array(x)
End If
Next
sender.text = text
sender.SelectionStart = text.Length
e.KeyChar = Chr(0)
End If
End If
Case Asc("0") To Asc("9") '" 0 to 9 "
e.KeyChar = e.KeyChar
Case Is = 44, 45 '","
Select Case TextLength
Case Is = 0
e.KeyChar = Chr(0)
Case Else
Select Case SelectionStart
Case 0
e.KeyChar = Chr(0)
Case 1 To TextLength - 1
If Mid(sender.text, SelectionStart, 1) = SplitStr Or Mid(sender.text, SelectionStart + 1, 1) = SplitStr Then
e.KeyChar = Chr(0)
Else
e.KeyChar = e.KeyChar
End If
Case TextLength
If Mid(sender.text, SelectionStart, 1) = SplitStr Then
e.KeyChar = Chr(0)
Else
e.KeyChar = e.KeyChar
End If
End Select
End Select
Case Else
e.KeyChar = Chr(0)
End Select
End Sub
這是我的程序中復(fù)制過來的,只能輸入數(shù)據(jù)字與逗號還有下劃線,你查一下F和J的Ass嗎是多少,改寫一下就OK
如果你用的鍵盤hook的話,你可以用msdn查詢一下里面包含了 keyboardproc回調(diào)函數(shù)的
參數(shù)的解釋?。∑渲?wparam就是所謂的 虛擬件 例如
VK_SPACED代表 空格
在vb中你可以 根據(jù) 定義宏或者常量來 給16進制的 數(shù)字賦值 然后舊可以和c一樣調(diào)用
vk_?拉, 加分謝謝!??!
加個Textbox(文本框), 如果它得到焦點后,和標準鍵盤一樣按USB鍵盤的鍵后就會觸發(fā)keyPress,keyUp,keyDown,這樣你就得到了keyAscii
在textbox中是你輸入的鍵, 在Frame1 上顯示的是最后輸入的鍵的Ascii值
(下列程序是測試過的)
Private Sub Text1_KeyPress(KeyAscii As Integer)
Frame1.Caption = Str(KeyAscii)
End Sub
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