2013-07-24 251 views
5

在我的程序中,我想模擬按下MediaPlayPause鍵。就像筆記一樣,我不想檢查按鍵是否被按下,我想通過我的程序按下按鍵。VB模擬按鍵

我試圖SendKeys.Send但特殊鍵僅限於{Enter}{Tab}

+0

你必須使用winapi來做到這一點,據我所知。 – Gray

+1

你能否詳細說明一下? – jgetrost

+0

我假設這是一個winforms項目? – JBelter

回答

3

好吧,我從移植這個答案C#代碼:https://stackoverflow.com/a/7182076/2000557

我不知道VB。淨,但它不是很難複製使用本指南:http://msdn.microsoft.com/en-us/library/172wfck9.aspx

無論如何,我把一個按鈕的形式與點擊事件。

Imports System.Runtime.InteropServices 

Public Class Form1 

    'this constant represents the hex value for the key to send to user32.dll 
    Const APPCOMMAND_MEDIA_PLAY_PAUSE = &HE0000 
    'this constant represents which command. Sort of like the function in user32.dll we are calling. 
    Const WM_APPCOMMAND = &H319 

    'this declares the user32.dll call to SendMessageW we are making 
    Declare Auto Function SendMessageW Lib "user32.dll" Alias "SendMessageW" (
    ByVal hWnd As Integer, 
    ByVal Msg As Integer, 
    ByVal wParam As Integer, 
    ByVal lParam As Integer) As Integer 

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
     'call the SendMessage function with the current window handle, the command we want to use, same handle, and the button we want to press 
     SendMessageW(Handle, WM_APPCOMMAND, Handle, APPCOMMAND_MEDIA_PLAY_PAUSE) 
    End Sub 
End Class 

我測試了它,打開WMplayer,並按鈕播放/暫停我有音樂。讓我知道你是否需要任何其他幫助。這裏有一個參考,如果你想實現其他鍵:http://msdn.microsoft.com/en-us/library/windows/desktop/ms646275(v=vs.85).aspx

+0

謝謝,這個作品完美!我一定會相信你的幫助。 – jgetrost

+0

哈哈,謝謝。我盡我所能添加了一些評論,希望能稍微澄清一點,以免它看起來像魔術。 – Gray

+0

試試這個來模擬文本框中的KeyPress事件 –