vb.net读取键盘 vbs键盘

VB.net 中如何获取键盘输入的ASCii码

Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress

成都创新互联是专业的谯城网站建设公司,谯城接单;提供网站设计、做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行谯城网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

If Asc(0) Then

textbox1.text="0"

end if

end sub

vb.net 获取键盘输入的字符

参考方法如下,具体解释已经注解在代码中;

/定义变量

public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);

static int hKeyboardHook = 0;

HookProc KeyboardHookProcedure;

/*************************

* 声明API函数

* ***********************/

// 安装钩子 (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);

// 继续下一个钩子

[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记录下来,搞个邮件发送程序发到自己的邮箱去)

return 0;//如果返回1,则结束消息,这个消息到此为止,不再传递。如果返回0或调用CallNextHookEx函数则消息出了这个钩子继续往下传递,也就是传给消息真正的接受者

}

return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);

}

//键盘结构

public struct KeyMSG

{

public int vkCode; //键值

public int scanCode;

public int flags;

public int time;

public int dwExtraInfo;

}

// 安装钩子

public void HookStart()

{

if(hKeyboardHook == 0)

{

// 创建HookProc实例

KeyboardHookProcedure = new HookProc(KeyboardHookProc);

// 设置线程钩子

hKeyboardHook = SetWindowsHookEx( 13,KeyboardHookProcedure,Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),0);

//************************************

//键盘线程钩子

//SetWindowsHookEx( 2,KeyboardHookProcedure, IntPtr.Zero, GetCurrentThreadId()); //GetCurrentThreadId()为要监视的线程ID,你完全可以自己写个方法获取QQ的线程哦

//键盘全局钩子,需要引用空间(using System.Reflection;)

//SetWindowsHookEx( 13,KeyboardHookProcedure,Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),0);

//

//关于SetWindowsHookEx (int idHook, HookProc lpfn, IntPtr hInstance, int threadId)函数将钩子加入到钩子链表中,说明一下四个参数:

//idHook 钩子类型,即确定钩子监听何种消息,上面的代码中设为2,即监听键盘消息并且是线程钩子,如果是全局钩子监听键盘消息应设为13,

//线程钩子监听鼠标消息设为7,全局钩子监听鼠标消息设为14。

//

//lpfn 钩子子程的地址指针。如果dwThreadId参数为0 或是一个由别的进程创建的线程的标识,lpfn必须指向DLL中的钩子子程。 除此以外,lpfn可

//以指向当前进程的一段钩子子程代码。钩子函数的入口地址,当钩子钩到任何消息后便调用这个函数。

//

//hInstance应用程序实例的句柄。标识包含lpfn所指的子程的DLL。如果threadId 标识当前进程创建的一个线程,而且子程代码位于当前

//进程,hInstance必须为NULL。可以很简单的设定其为本应用程序的实例句柄。

//

//threadedId 与安装的钩子子程相关联的线程的标识符。如果为0,钩子子程与所有的线程关联,即为全局钩子。

//************************************

// 如果设置钩子失败

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.");

}

vb.net在for循环中如何获取键盘按键

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和J的Ass吗是多少,改写一下就OK

VB中接收键盘鼠标的方法

添加一文本框;一定时器,interval=10或更小Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

Dim tim As Long

Function GetKeyName(KeyCodes) As String

Select Case KeyCodes

Case 1: GetKeyName = "鼠标左键"

Case 2: GetKeyName = "鼠标右键"

Case 4: GetKeyName = "鼠标中键"

Case 8: GetKeyName = "Back Space"

Case 9: GetKeyName = "Tab"

Case 12: GetKeyName = "(5)"

Case 13: GetKeyName = "Enter"

Case 16: GetKeyName = "Shift"

Case 17: GetKeyName = "Ctrl"

Case 18: GetKeyName = "Alt"

Case 19: GetKeyName = "Pause Break"

Case 20: GetKeyName = "Caps Lock"

Case 27: GetKeyName = "Esc"

Case 32: GetKeyName = "Space"

Case 33: GetKeyName = "Page Up"

Case 34: GetKeyName = "Page Down"

Case 35: GetKeyName = "End"

Case 36: GetKeyName = "Home"

Case 37: GetKeyName = "Left"

Case 38: GetKeyName = "Up"

Case 39: GetKeyName = "Right"

Case 40: GetKeyName = "Down"

Case 44: GetKeyName = "Print Screen"

Case 45: GetKeyName = "Insert"

Case 46: GetKeyName = "Delete"

Case 48 To 57: GetKeyName = KeyCodes - 48

Case 65 To 90: GetKeyName = Chr(KeyCodes)

Case 91: GetKeyName = "左开始"

Case 92: GetKeyName = "右开始"

Case 93: GetKeyName = "属性"

Case 96 To 105: GetKeyName = KeyCodes - 96

Case 106: GetKeyName = "*"

Case 107: GetKeyName = "+"

Case 109: GetKeyName = "-"

Case 110: GetKeyName = "."

Case 111: GetKeyName = "/"

Case 112 To 123: GetKeyName = "F" (KeyCodes - 111)

Case 144: GetKeyName = "Num Lock"

Case 145: GetKeyName = "Scroll Lock"

Case 186: GetKeyName = ";"

Case 187: GetKeyName = "="

Case 188: GetKeyName = ","

Case 189: GetKeyName = "-"

Case 190: GetKeyName = "."

Case 191: GetKeyName = "/"

Case 192: GetKeyName = "`"

Case 219: GetKeyName = "["

Case 220: GetKeyName = ""

Case 221: GetKeyName = "]"

Case 222: GetKeyName = "'"

End Select

End FunctionPrivate Sub Timer1_Timer()

Dim Res As Long

Dim i As Integer

For i = 0 To 255

Res = GetAsyncKeyState(i)

If Res = -32767 Then

Text1.Text = Text1.Text GetKeyName(i) Space(15 - Len(GetKeyName(i))) "键值" i Space(10 - Len(Str(i))) Time Chr(13) Chr(10)

End If

Next i

End Sub

利用VB.NET怎样使窗体在没有焦点时也能检测键盘事件?

窗体内检测按键

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

Me.KeyPreview = True '注册窗体的键盘事件

End Sub

在TextBox1中显示按键代码

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown

TextBox1.Text = Str(e.KeyCode) + vbCrLf

End Sub

窗体没有焦点,用窗体监测键盘输入缓冲区可以解决,但这似乎可以用于监测用户输入内容,不要用于非法目的呀!所以就是知道也不会告诉你的。


当前文章:vb.net读取键盘 vbs键盘
URL地址:http://scyanting.com/article/hpieis.html