我写个程序,实现在按钮上可以用键盘操作,但是却不知道为什么执行不了:
private sub form_load()
command1(0).setfocus
end sub
private sub command1(0)_keypress(....)
if key = vbKeyW then
command1(0).caption = "asdf"
end if
end sub
请问大家怎么回事啊?执行的时候按下W键为什么没用啊?~~

解决方案 »

  1.   

    写在Form_KeyPress里面:
    If keyCode = VBKeyW Then
        Me.ActiveControl.Caption = "asdf"
    End If
    设置窗口的KeyPreview为True
      

  2.   

    [Quote=引用 3 楼 caozhy 的回复:]引用 2 楼 frankarmageddon 的回复:
    引用 1 楼 caozhy 的回复:写在Form_KeyPress里面:
    If keyCode = VBKeyW Then
    Me.ActiveControl.Caption = "asdf"
    End If
    设置窗口的KeyPreview为True试过,没用~~重试。
    [/Quo好吧
      

  3.   


    e.ActiveControl.Caption = "asdf"没法确定是command数组中的哪一个啊??
      

  4.   

    private sub command1(0)_keypress(....)
    省略的括号里面的东西很重要,是传递的参数,如index、keycode等
      

  5.   


    keyascii,显然的啦
    但是还是不行~~搞不懂啊?
      

  6.   

    好像是Command1_KeyPress(Index, KeyPress)
    怎么是Command(0)_KeyPress() 呢?
      

  7.   

    Private Sub Command1_KeyPress(Index As Integer, KeyAscii As Integer)
    If KeyAscii = vbKeyW Or KeyAscii = 119 Then
    Command1(0).Caption = "asdf"
    End If
    End Sub大小写的问题,大写的时候才是 vbKeyW
      

  8.   

    换成 KeyDown 或 KeyUp 则不区分大小写
    Private Sub Command1_KeyDown(Index As Integer, KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyW Then
     Command1(0).Caption = "asdf"
    End If
    End Sub这样也行 
    Private Sub Command1_KeyPress(Index As Integer, KeyAscii As Integer)
    If UCase(Chr(KeyAscii)) = "W" Then
    Command1(0).Caption = "asdf"
    End If
    End Sub
      

  9.   

    Private Sub Command1_KeyPress(Index As Integer, KeyAscii As Integer)
     msgbox KeyAscii 
    End Sub
    你看看是什么?
      

  10.   

    那不就得了。vbKeyW  值是87 
    我10楼的代码有什么问题?