Vb.net VB模拟按键

Vb.net VB模拟按键,vb.net,sendkeys,simulate,Vb.net,Sendkeys,Simulate,在我的程序中,我想模拟按下MediaPlayPause键。请注意,我不想检查按键是否按下,我想通过我的程序按键 我尝试过SendKeys.Send,但特殊键仅限于{Enter}和{Tab}等。好的,我从以下答案移植了c代码: 我不知道VB.Net,但使用本指南进行复制并不难: 不管怎样,我在表单上添加了一个带有单击事件的按钮 Imports System.Runtime.InteropServices Public Class Form1 'this constant represe

在我的程序中,我想模拟按下
MediaPlayPause
键。请注意,我不想检查按键是否按下,我想通过我的程序按键

我尝试过SendKeys.Send,但特殊键仅限于
{Enter}
{Tab}
等。

好的,我从以下答案移植了c代码:

我不知道VB.Net,但使用本指南进行复制并不难:

不管怎样,我在表单上添加了一个带有单击事件的按钮

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进行了测试,按钮播放/暂停了我播放的音乐。如果你需要其他帮助,请告诉我。如果你想实现其他键,这里有一个参考:

据我所知,你必须使用winapi来实现这一点。我假设这是一个winforms项目?是的,它是一个winforms应用程序。这可能会有所帮助,尽管我不确定你可以将枚举传递到哪个类/方法:@jgetrost抱歉,你在那里要求一些解释当然是对的。基本上,我的问题是我不太了解vb.net。我在这里找到了c的答案。也许了解VB.net的人(或您)可以将其转换为可用的。谢谢,这非常有效!我相信你的帮助。哈哈,谢谢。我尽了最大努力添加了一些注释,希望能稍微澄清一下,这样看起来就不那么神奇了