重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
微软的DirectInput是基于硬件底层,可以在窗口无焦点时捕捉按键,有兴趣可以去了解了解,在游戏制作方面很有用。
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:空间域名、网页空间、营销软件、网站建设、安陆网站维护、网站推广。
DirectX SDK 中文版,里面有关于DirectInput的介绍与用法。
窗体内检测按键
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
窗体没有焦点,用窗体监测键盘输入缓冲区可以解决,但这似乎可以用于监测用户输入内容,不要用于非法目的呀!所以就是知道也不会告诉你的。
Public Class Form2
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
AddHandler Label1.MouseEnter, AddressOf LabelMouseEnter
AddHandler Label2.MouseEnter, AddressOf LabelMouseEnter
AddHandler Label1.MouseLeave, AddressOf LabelMouseLeave
AddHandler Label2.MouseLeave, AddressOf LabelMouseLeave
AddHandler Label1.MouseDown, AddressOf LabelMouseDown
AddHandler Label2.MouseDown, AddressOf LabelMouseDown
End Sub
Private Sub LabelMouseEnter(ByVal sender As Object, ByVal e As System.EventArgs)
Dim c As Label = DirectCast(sender, Label)
c.BackColor = Color.DodgerBlue
End Sub
Private Sub LabelMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim c As Label = DirectCast(sender, Label)
Form1.TextBox1.Text = c.Text
End Sub
Private Sub LabelMouseLeave(ByVal sender As Object, ByVal e As System.EventArgs)
Dim c As Label = DirectCast(sender, Label)
c.BackColor = SystemColors.Control
End Sub
End Class
最简单的加速运动示例,直接拷贝代码,即可用方向键控制控件移动
Public Class 最简单的加速运动
Dim 左右, 上下 As Integer
Dim X, Y, VX, VY, VA As Double
Dim WithEvents 时钟 As New Timer
Dim WithEvents 移动控件 As New Label
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True
VA = 0.5
X = 300.0
Y = 300.0
移动控件.BackColor = Color.MediumPurple
移动控件.Size = New Size(60, 60)
Controls.Add(移动控件)
时钟.Interval = 25
时钟.Enabled = True
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Up Then 上下 = -1
If e.KeyCode = Keys.Down Then 上下 = 1
If e.KeyCode = Keys.Left Then 左右 = -1
If e.KeyCode = Keys.Right Then 左右 = 1
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
If e.KeyCode = Keys.Up Then 上下 = 0
If e.KeyCode = Keys.Down Then 上下 = 0
If e.KeyCode = Keys.Left Then 左右 = 0
If e.KeyCode = Keys.Right Then 左右 = 0
End Sub
Private Sub 时钟_Tick(sender As Object, e As EventArgs) Handles 时钟.Tick
VX = Math.Min(VX + 左右 * VA, 10)
VY = Math.Min(VY + 上下 * VA, 10)
X += VX
Y += VY
If X 0 Then X = 2 : VX = -VX
If Y 0 Then Y = 2 : VY = -VY
If X Width - 移动控件.Width Then X = Width - 移动控件.Width - 2 : VX = -VX
If Y Height - 移动控件.Height Then Y = Height - 移动控件.Height - 2 : VY = -VY
移动控件.Location = New Point(X, Y)
End Sub
End Class