Private?Sub?Form1_KeyPress(ByVal?sender?As?Object,?ByVal?e?As?System.Windows.Forms.KeyPressEventArgs)?Handles?Me.KeyPress
創(chuàng)新互聯(lián)建站是由多位在大型網(wǎng)絡(luò)公司、廣告設(shè)計(jì)公司的優(yōu)秀設(shè)計(jì)人員和策劃人員組成的一個(gè)具有豐富經(jīng)驗(yàn)的團(tuán)隊(duì),其中包括網(wǎng)站策劃、網(wǎng)頁(yè)美工、網(wǎng)站程序員、網(wǎng)頁(yè)設(shè)計(jì)師、平面廣告設(shè)計(jì)師、網(wǎng)絡(luò)營(yíng)銷人員及形象策劃。承接:成都網(wǎng)站建設(shè)、成都做網(wǎng)站、網(wǎng)站改版、網(wǎng)頁(yè)設(shè)計(jì)制作、網(wǎng)站建設(shè)與維護(hù)、網(wǎng)絡(luò)推廣、數(shù)據(jù)庫(kù)開發(fā),以高性價(jià)比制作企業(yè)網(wǎng)站、行業(yè)門戶平臺(tái)等全方位的服務(wù)。
If?Asc(0)?Then
textbox1.text="0"
end?if
end?sub
參考方法如下,具體解釋已經(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ù)下一個(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)聽何種消息,上面的代碼中設(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 或是一個(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.");
}
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ù)制過(guò)來(lái)的,只能輸入數(shù)據(jù)字與逗號(hào)還有下劃線,你查一下F和J的Ass嗎是多少,改寫一下就OK