返回列表 上一主題 發帖

請問如何用鍵盤鍵跟CommandButton有相同功能

請問如何用鍵盤鍵跟CommandButton有相同功能

本帖最後由 cdkee 於 2010-10-6 18:00 編輯

Public i As Integer

Private Sub CommandButton1_Click()

i = i + 1
        Sheets("sheet1").Cells(i, 1) = "X="
        Sheets("sheet1").Cells(i, 2) = Int((10 * Rnd) + 1)

End Sub

請教大大,按鍵盤鍵(例如"F1")跟按CommandButton1有相同功能,是怎樣寫成的?謝謝!

Public i As Integer

Private Sub CommandButton1_Click()

Application.OnKey "{F1}"
i = i + 1
        Sheets("sheet1").Cells(i, 1) = "X="
        Sheets("sheet1").Cells(i, 2) = Int((10 * Rnd) + 1)

End Sub

試了這個,但不成功...

TOP

Private Sub CommandButton1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
找到這個,但不懂有沒有關係...

TOP

Public i As Integer
Private Sub CommandButton1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = Asc("{F1}") Then
CommandButton1_Click
End If
End Sub


Private Sub CommandButton1_Click()
i = i + 1

        Sheets("sheet1").Cells(i, 1) = "X="
        Sheets("sheet1").Cells(i, 2) = Int((10 * Rnd) + 1)

End Sub
試了這個,也不成功...

TOP

回復 5# GBKEE


    Public i As Integer
Sub Ex()
Application.OnKey "{F1}", "Sheet1.CommandButton1_Click"
End Sub

Private Sub CommandButton1_Click()

i = i + 1

        Sheets("sheet1").Cells(i, 1) = "X="
        Sheets("sheet1").Cells(i, 2) = Int((10 * Rnd) + 1)

End Sub
謝謝版大!但還是不成...小輩還有什麼錯?

TOP

回復  cdkee
Sub Ex()
Application.OnKey "{F1}", "Sheet1.CommandButton1_Click"
End Sub

Private  ...
GBKEE 發表於 2010-10-6 21:15



    Sheet1是私用程序,要修改那些?謝謝版大!
這要用API的Hook(鉤子函數)來監視你的鍵盤操作,比較麻煩,很多木馬就使用了這個技術來得到你輸入的密碼、帳 ...
et5323 發表於 2010-10-6 21:26

大大是說沒有API就不行?

TOP

1. In design mode, select your CommandButton1;
2. In the Properties window, enter a single letter for the field Accelerator;

Suppose the caption of your CommandButton1 is “Fire” and a letter “r” is typed in step 2, then letter “r” will be underlined.
Other letters will be accepted but the underline will not shown in the caption.
Also note that it is different for lower and upper case letters.

To run your subroutine, press ALT+r or click CommandButton1 for the above example.
找到像樣的,但要按2個KEY,及FUNCTION KEY不能利用!
請教大大怎樣改,也可會做到?

TOP

et5323是說少用API函數
請看附檔
GBKEE 發表於 2010-10-7 07:27



    謝謝版大!先下蛓來學習...

TOP

et5323是說少用API函數
請看附檔
GBKEE 發表於 2010-10-7 07:27



    跟版大的方法成功了,但小輩仍在思考...這方式有什麼不好?大大們好像不大建議,是小輩小見,再請大大們教導。

TOP

回復 14# GBKEE


    謝謝版大多方提點!

TOP

        靜思自在 : 有願放在心裡,沒有身體力行,正如耕田不播種,皆是空過因緣。
返回列表 上一主題