【问题标题】:VB Simulate a key pressVB模拟按键
【发布时间】:2013-07-24 10:28:49
【问题描述】:

在我的程序中,我想模拟按下MediaPlayPause 键。就像一个注释,我不想检查键是按下还是按下,我想通过我的程序按下键。

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

【问题讨论】:

  • 据我所知,您必须使用 winapi 来执行此操作。
  • 我假设这是一个 winforms 项目?
  • 是的,它是一个winforms应用程序。
  • 这可能会有所帮助,尽管我不确定您可以将枚举传递给哪个类/方法:MSDN Keys Enumeration
  • @jgetrost 抱歉,您当然可以在那里寻求一些解释。基本上,我的问题是我不太了解 vb.net。我在这里找到了 c# 的答案:stackoverflow.com/questions/7181978/special-keys-on-keyboards。也许了解 VB.net(或您)的人可以将其转换为可用的。

标签: vb.net winforms sendkeys simulate


【解决方案1】:

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

我不了解 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

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

【讨论】:

  • 谢谢你,这很好用!我一定会感谢你的帮助。
  • 哈哈,谢谢。我尽力添加了一些 cmets,希望能稍微清理一下,让它看起来不像魔法。
  • 尝试在文本框中模拟 KeyPress 事件
猜你喜欢
  • 2015-08-24
  • 1970-01-01
  • 1970-01-01
  • 2012-01-31
  • 2020-04-29
  • 2011-03-04
  • 2010-11-22
  • 2011-11-10
相关资源
最近更新 更多