VB模拟按键

jge*_*ost 5 vb.net simulate sendkeys

在我的程序中,我想模拟MediaPlayPause按键.就像一张纸条,我不想检查按键是否按下或按下,我想通过我的程序按键.

我曾尝试SendKeys.Send但特殊键限于{Enter}{Tab}

Gra*_*ray 3

好的,我从这个答案移植了 C# 代码:/sf/answers/502745351/

我不知道 VB.Net,但使用本指南复制并不太难: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
Run Code Online (Sandbox Code Playgroud)

我通过打开 WMplayer 进行测试,按钮播放/暂停我的音乐。如果您需要任何其他帮助,请告诉我。如果您想实现其他键,请参考以下内容:http://msdn.microsoft.com/en-us/library/windows/desktop/ms646275 (v=vs.85).aspx