| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1827 人关注过本帖
标题:大家好请教一个VB按键判定问题 和 音乐播放问题
只看楼主 加入收藏
redice
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:902
专家分:0
注 册:2006-12-11
收藏
得分:0 

这是一个类似键盘记录的程序,窗体上需要一个label控件和一个timer控件
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

Private Sub Form_Load()
Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
Dim AddKey
KeyResult = GetAsyncKeyState(13) '回车键
If KeyResult = -32767 Then
AddKey = "[ENTER]"
GoTo KeyFound
End If
KeyResult = GetAsyncKeyState(17) 'Ctrl键
If KeyResult = -32767 Then
AddKey = "[CTRL]"
GoTo KeyFound
End If
KeyResult = GetAsyncKeyState(8) '退格键
If KeyResult = -32767 Then
AddKey = "[BKSPACE]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(9)
If KeyResult = -32767 Then
AddKey = "[TAB]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(18)
If KeyResult = -32767 Then
AddKey = "[ALT]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(19)
If KeyResult = -32767 Then
AddKey = "[PAUSE]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(20)
If KeyResult = -32767 Then
AddKey = "[CAPS]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(27)
If KeyResult = -32767 Then
AddKey = "[ESC]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(33)
If KeyResult = -32767 Then
AddKey = "[PGUP]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(34)
If KeyResult = -32767 Then
AddKey = "[PGDN]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(35)
If KeyResult = -32767 Then
AddKey = "[END]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(36)
If KeyResult = -32767 Then
AddKey = "[HOME]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(44)
If KeyResult = -32767 Then
AddKey = "[SYSRQ]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(45)
If KeyResult = -32767 Then
AddKey = "[INS]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(46)
If KeyResult = -32767 Then
AddKey = "[DEL]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(144)
If KeyResult = -32767 Then
AddKey = "[NUM]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(37)
If KeyResult = -32767 Then
AddKey = "[align=LEFT]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(38)
If KeyResult = -32767 Then
AddKey = "[UP]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(39)
If KeyResult = -32767 Then
AddKey = "[align=RIGHT]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(40)
If KeyResult = -32767 Then
AddKey = "[DOWN]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(112)
If KeyResult = -32767 Then
AddKey = "[F1]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(113)
If KeyResult = -32767 Then
AddKey = "[F2]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(114)
If KeyResult = -32767 Then
AddKey = "[F3]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(115)
If KeyResult = -32767 Then
AddKey = "[F4]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(116)
If KeyResult = -32767 Then
AddKey = "[F5]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(117)
If KeyResult = -32767 Then
AddKey = "[F6]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(118)
If KeyResult = -32767 Then
AddKey = "[F7]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(119)
If KeyResult = -32767 Then
AddKey = "[F8]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(120)
If KeyResult = -32767 Then
AddKey = "[F9]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(121)
If KeyResult = -32767 Then
AddKey = "[F10]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(122)
If KeyResult = -32767 Then
AddKey = "[F11]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(123)
If KeyResult = -32767 Then
AddKey = "[F12]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(124)
If KeyResult = -32767 Then
AddKey = "[F13]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(125)
If KeyResult = -32767 Then
AddKey = "[F14]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(126)
If KeyResult = -32767 Then
AddKey = "[F15]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(127)
If KeyResult = -32767 Then
AddKey = "[F16]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(32)
If KeyResult = -32767 Then
AddKey = " "
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(186)
If KeyResult = -32767 Then
AddKey = ";"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(187)
If KeyResult = -32767 Then
AddKey = "="
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(188)
If KeyResult = -32767 Then
AddKey = ","
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(189)
If KeyResult = -32767 Then
AddKey = "-"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(190)
If KeyResult = -32767 Then
AddKey = "."
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(191)
If KeyResult = -32767 Then
AddKey = "/" '/
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(192)
If KeyResult = -32767 Then
AddKey = "`" '`
GoTo KeyFound
End If

'----------NUM PAD
KeyResult = GetAsyncKeyState(96)
If KeyResult = -32767 Then
AddKey = "0"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(97)
If KeyResult = -32767 Then
AddKey = "1"
GoTo KeyFound
End If


KeyResult = GetAsyncKeyState(98)
If KeyResult = -32767 Then
AddKey = "2"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(99)
If KeyResult = -32767 Then
AddKey = "3"
GoTo KeyFound
End If


KeyResult = GetAsyncKeyState(100)
If KeyResult = -32767 Then
AddKey = "4"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(101)
If KeyResult = -32767 Then
AddKey = "5"
GoTo KeyFound
End If


KeyResult = GetAsyncKeyState(102)
If KeyResult = -32767 Then
AddKey = "6"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(103)
If KeyResult = -32767 Then
AddKey = "7"
GoTo KeyFound
End If


KeyResult = GetAsyncKeyState(104)
If KeyResult = -32767 Then
AddKey = "8"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(105)
If KeyResult = -32767 Then
AddKey = "9"
GoTo KeyFound
End If


KeyResult = GetAsyncKeyState(106)
If KeyResult = -32767 Then
AddKey = "*"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(107)
If KeyResult = -32767 Then
AddKey = "+"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(108)
If KeyResult = -32767 Then
AddKey = "[ENTER]"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(109)
If KeyResult = -32767 Then
AddKey = "-"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(110)
If KeyResult = -32767 Then
AddKey = "."
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(2)
If KeyResult = -32767 Then
AddKey = "/"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(220)
If KeyResult = -32767 Then
AddKey = "\"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(222)
If KeyResult = -32767 Then
AddKey = "'"
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(221)
If KeyResult = -32767 Then
AddKey = "]"


GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(219)
If KeyResult = -32767 Then
AddKey = "["
GoTo KeyFound
End If

KeyResult = GetAsyncKeyState(16) 'shift键
If KeyResult = -32767 And TimeOut = 0 Then
AddKey = "[SHIFT]"
LastKey = AddKey
TimeOut = 1
GoTo KeyFound
End If

KeyLoop = 41

Do Until KeyLoop = 256 ' 显示其他键
KeyResult = GetAsyncKeyState(KeyLoop)
If KeyResult = -32767 Then Label1.Caption = Label1.Caption & Chr(KeyLoop)
KeyLoop = KeyLoop + 1
Loop
LastKey = AddKey
Exit Sub
KeyFound: '显示键的信息
Label1.Caption = Label1.Caption & AddKey
End Sub


鲲鹏数据 - 专业Web数据采集服务提供者
http://www.
2006-12-18 22:57
redice
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:902
专家分:0
注 册:2006-12-11
收藏
得分:0 
关于播放声音文件,我推荐使用API函数sndPlaySound(不过它只能播放WAV格式的声音)
这是函数声明:
Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
下面是使用改函数会用道的两个常量
Public Const SND_ASYND = &H1
Public Const SND_NODEFAULT = &H2
下面是一个例子(完整的代码):
Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Private Const SND_ASYND = &H1
Private Const SND_NODEFAULT = &H2
Private Sub Command1_Click()
sndPlaySound "C:\WINDOWS\Media\ringin.wav", SND_ASYND Or SND_NODEFAULT
End Sub

鲲鹏数据 - 专业Web数据采集服务提供者
http://www.
2006-12-18 23:04
快速回复:大家好请教一个VB按键判定问题 和 音乐播放问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017790 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved