注册 登录
编程论坛 VB6论坛

关于(KeyAscii 码转换的问题)~请各位师傅进来帮帮忙!!

qiziyun 发布于 2013-01-18 07:54, 751 次点击
程序代码:
Private Declare Function GetKeyNameText Lib "user32" Alias "GetKeyNameTextA" (ByVal lParam As Long, ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

Dim s As String

s = String(100, Chr(0))

GetKeyNameText MapVirtualKey(KeyCode, 0) * 65536, s, 100

s = Left(s, InStr(s, Chr(0)))

Me.Cls

Print s
End Sub
只有本站会员才能查看附件,请 登录


我用的源码是这里



KeyAscii 替换上去就提示错误
是不是声明限制了!!
还有运行不正常!应该怎么写才对呢!

我想实现的是鼠标放到Text文本框里按下键盘任何一个按钮就能在Text1里显示出对应的Alias码!列如我输入1就显示49
Text2 是显示对应的KeyDown识别的案件字符 列如a就显示A或a 尝试了很多次了就是不行!!各位谁看到知道的给个列子!谢谢了!!!!!

只有本站会员才能查看附件,请 登录
方便各位老师了!现成的附件!帮我改改谢谢了
11 回复
#2
bczgvip2013-01-18 11:50
程序代码:

Private Declare Function GetKeyNameText Lib "user32" Alias "GetKeyNameTextA" (ByVal lParam As Long, ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)

Dim s As String

s = String$(255, vbNullChar)

GetKeyNameText MapVirtualKey(KeyCode, 0) * 65536, s, 255

s = Left$(s, InStr(s, vbNullChar) - 1)

Me.Cls

Text2.Text = s
Print keycode,s
End Sub

#3
qiziyun2013-01-18 22:57
回复 2楼 bczgvip
    bczgvip   还在吗!!KeyAscii码不能换吗!!要怎么在目前这个列子上来声明和修改呢!!!!
#4
bczgvip2013-01-19 00:54
改改不就行了?话说【Alias码】是神马?
程序代码:
Option Explicit
Private Declare Function GetKeyNameText Lib "user32" Alias "GetKeyNameTextA" (ByVal lParam As Long, ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long

Private Sub Text1_KeyPress(KeyAscii As Integer)
    Dim s As String
    s = String$(255, vbNullChar)
    GetKeyNameText MapVirtualKey(KeyAscii, 0) * 65536, s, 255
    s = Left$(s, InStr(s, vbNullChar) - 1)
    Me.Cls
    Text2.Text = s
    Print KeyAscii, s
End Sub
#5
qiziyun2013-01-20 03:47
回复 4楼 bczgvip
bczgvip  很感谢你!,我还想问个小问题!!我怎么让他从TEXT文本框显示出来以后!如果矮子连着往TEXT1里输入按键,他居然叠加在一起。

只有本站会员才能查看附件,请 登录

按下键盘1对应的是49
返回的对应码是对的。
就是框里会保留原始记录,
第二次按的话就会叠加在一起变成
4949 或者949 我试着+了些删除前面文字的代码
就是不行,一定是我书写不对。应该怎么写才对呢,麻烦你了
#6
qiziyun2013-01-20 06:24
回复 4楼 bczgvip
可用来写后台操作模拟内存占用很小
#7
lowxiong2013-01-20 09:05
不知道楼主要完成什么功能,如果仅需要在text1中按键显示的是对应的ascii码的话,根本无需声明api函数即可完成,代码如下:
程序代码:
'复制下列代码后,在text1中按键即可看到效果
Private Sub Form_Load()
  Text1 = ""
  Text2 = ""
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
  Dim s As String, i As Integer
  If KeyAscii = 8 And Len(Text2) > 0 Then
    '处理退格键
    s = Asc(Right(Text2, 1))
    Text2 = Left(Text2, Len(Text2) - 1)
    Text1 = Left(Text1, Len(Text1) - Len(s))
    If Len(Text1) > 0 Then Text1.SelStart = Len(Text1)
    KeyAscii = 0
  End If
  If KeyAscii < 30 And KeyAscii >= 0 Then Exit Sub    '小于30大雨或等于0的大多为控制字符
  Text2 = Text2 & Chr(KeyAscii)
  Text1 = Text1 & Trim(Str(KeyAscii))
  Text1.SelStart = Len(Text1)
  KeyAscii = 0
End Sub


[ 本帖最后由 lowxiong 于 2013-1-20 09:21 编辑 ]
#8
yz10252013-01-21 10:18
以下是引用qiziyun在2013-1-18 07:54:06的发言:


这个布景主题好熟悉..
#9
qiziyun2013-01-21 23:13
回复 8楼 yz1025
什么布景啊!????你是说我发帖的习惯吗!呵呵
#10
qiziyun2013-01-21 23:16
我需要显示ascii码的你这个不能显示没声明是吧??
#11
lowxiong2013-01-22 00:39
你是不是根本就没运行7楼代码?如果你需要的是在text1中按键时显示对应asc码,同时在text2中显示键入的字符,那么7楼代码即可完成的,效果见下图(汉字内码也显示了,负数的都是汉字内码,最后两个asc码是63和49,分别对应?和1):
只有本站会员才能查看附件,请 登录
#12
yz10252013-01-22 11:24
以下是引用qiziyun在2013-1-21 23:13:59的发言:

什么布景啊!????你是说我发帖的习惯吗!呵呵


不是~是UI的布景~
只有本站会员才能查看附件,请 登录
1