2013-07-24 254 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事件 –