计算机二级vb程序设计教程第10章键盘与鼠标事件

3 篇文章 0 订阅

本章介绍和键盘鼠标有光的事件过程

KeyPress事件

当压下键盘上的某个键的时候,将发生KeyPress事件
精确描述: 按下某个键,我们将触发此时拥有焦点的KeyPress 事件。
输入焦点只能位于一个控件上,如果窗体没有活动或者可见的控件,则输入焦点位于窗体。
我们可以利用vb自动生成一个 这样的keypress事件
在这里插入图片描述

说明:
- KeyASCII 用于单个控件。 keyAscii 是 所按键的ASCII码
例子

   Private Sub Text1_Key Press(KeyAscii As Integer)
      if KeyAscii < 48 Or KeyAscii > 57 Then
      Beep
      KeyAscii = 0
  End Sub

在这里插入图片描述
只允许输入 0 (48) ~ 9(57)

Private Sub Text1_KeyPress(KeyAscii As Integer)
  If KeyAscii < 48 Or KeyAscii > 57 Then
    Beep
    KeyAscii = 0
End If
KeyAscii = 0
End Sub


Private Sub Text2_KeyPress(KeyAscii As Integer)
If KeyAscii >= 65 And KeyAscii <= 122 Then
  KeyAscii = 42
End If
End Sub

Private Sub Text3_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
  Printer.Print Text3.Text
End If
KeyAscii = 0

学习数据库的时候,视屏介绍了 一个方法叫做 抽象化和实例化比较法
10.1 编写口令程序

Private Sub Form_Load()
  Text1.Text = ""
  	Text1.FontSize = 10 
  	 Label1.FontSize 12
  	  Label1.FontBold = True
  	  Label1.FontName = "隶书"
  	  Label1.Caption = "请输入口令"
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
Private Sub Text1_KeyPress(KeyAscii As Integer)
Static PWord As String
Static Counter As Integer
Static Numberoftries As Integer
If Numberoftries = 12 Then End
Counter = Counter + 1
PWord = PWord + Chr$(KeyAscii)
KeyAscii = 0
Text1.Text = String$(Counter, "*")
If LCase$(PWord) = "abcd" Then
  Text1.Text = ""
  PWord = 0
  MsgBox "口令正确"
  Counter = 0
  Print "Continue"
ElseIf Counter = 4 Then
  Counter = 0
  PWord = ""
  Text1.Text = ""
  MsgBox "口令对不对,请从星输入"
End If
End Sub

KeyDown KeyUp 事件

KeyDown 和 KeyUp返回的是键盘的状态,
过程的参数用于的那个控件,KeyCode As Integer Shift As Integer
KeyCode 大写字母和小写字母 是一样的, 因为都用一个键盘

SHIFT
转换键 ,shift ctrl AL他 001 010 100
我们可以建立如下过程
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)

End Sub
KeyDown 是键盘按下时产生的事件,KeyUp是键盘松开时产生的事件

例子
按下的时候标签显示扫描码,松开的时候被清楚

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
   Label1.Caption = Str$(KeyCode)
End Sub

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
  Label.Caption = ""
End Sub

我们可以用
Const Shift = 1
Const Ctrl = 2
Const Alt = 4
shift And Shift > 0
Shift And Ctrl > 0
Shift And Alt > 0


鼠标事件
单击和双击
按下和放开

按下
MouseDown
松开
MouseUp
移动
MouseMove

参数(Button As Integer,Shift As Integer,x As Single, y As Single)
Button 按下的鼠标键
SHift 表示 shift Ctrl Alt状态
x y 鼠标光标的当前位置

鼠标光标的形状

MousePointer
设置鼠标光标形状

  1. 对象.MousePoint = 设置值

windows 规则
1. 表示用户可用的功能

拖放

属性
DragMode
DragIcon
手动拖放
自动拖放

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值