VB模拟按键 [英] VB Simulate a key press

查看:34
本文介绍了VB模拟按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中,我想模拟按下 MediaPlayPause 键.作为说明,我不想检查键是按下还是按下,我想通过我的程序按下键.

In my program, I want to simulate the pressing of the MediaPlayPause key. Just as a note, I do not want to check to see if the key is down or pressed, I want to press the key via my program.

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

I have tried SendKeys.Send but the special keys are limited to {Enter} and {Tab}, etc.

推荐答案

好的,我从这个答案中移植了 c# 代码:https://stackoverflow.com/a/7182076/2000557

Ok, I ported the c# code from this answer: https://stackoverflow.com/a/7182076/2000557

我不知道 VB.Net,但使用本指南复制并不太难:http://msdn.microsoft.com/en-us/library/172wfck9.aspx

I don't know VB.Net, but it wasn't too hard to copy over using this guide: http://msdn.microsoft.com/en-us/library/172wfck9.aspx

无论如何,我在表单上放置了一个带有点击事件的按钮.

Anyway, I put a button on the form with a click event.

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

I tested it by opening WMplayer, and the button play/paused the music I had. Let me know if you need any other help. Here's a reference if you want to implement other keys: http://msdn.microsoft.com/en-us/library/windows/desktop/ms646275(v=vs.85).aspx

这篇关于VB模拟按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆