VBA Excel键检测 [英] VBA excel keydetection

查看:36
本文介绍了VBA Excel键检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天从VBA开始,我正在做蛇.

I started with VBA today and I'm making snake.

我遇到了一个小问题:excell确实没有简单的密钥检测方法

I have encountered a slight problem: excell doesn't really have an easy key detection method

解决这个问题的最好方法是什么?

what's the best way of dealing with this problem?

如果您要回答:请使用onkey方法:

if you are going to answer: use the onkey method:

我无法完成这项工作,因为我需要一个新的子对象,我要在其中提供方向数组

I can't make this work because I need a new sub in which I want to give my direction array

(暗淡的方向(2)为整数方向(1)为x移动方向(2)为Y移动)

(dim direction(2) as integer direction(1) being the x movement direction(2) being the Y movement)

这不起作用,因为新的子目录无法编辑另一个子目录的变量

this doesn't work because the new sub can't edit another sub's variables

我该怎么做?

推荐答案

子项 KeyTest 将测试是否已按下 QWERTY 键.

The sub KeyTest will test if the QWERTY keys have been pressed.

完成将停止 KeyTest

Public ImDone As Boolean
Public Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

Sub KeyTest()
    ImDone = False
    Do Until ImDone
        DoEvents
        If (GetAsyncKeyState(vbKeyQ)) Then
            MsgBox "Got a q"
            Exit Sub
        End If
        If (GetAsyncKeyState(vbKeyW)) Then
            MsgBox "Got a w"
            Exit Sub
        End If
        If (GetAsyncKeyState(vbKeyE)) Then
            MsgBox "Got an e"
            Exit Sub
        End If
        If (GetAsyncKeyState(vbKeyR)) Then
            MsgBox "Got an r"
            Exit Sub
        End If
        If (GetAsyncKeyState(vbKeyT)) Then
            MsgBox "Got a t"
            Exit Sub
        End If
        If (GetAsyncKeyState(vbKeyY)) Then
            MsgBox "Got a y"
            Exit Sub
        End If
    Loop
    MsgBox "done"
End Sub

Sub Finish()
    ImDone = True
End Sub

这篇关于VBA Excel键检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆