5

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.

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

DarkTrick
  • 2,447
  • 1
  • 21
  • 39
jgetrost
  • 193
  • 4
  • 12
  • You'll have to use winapi to do this as far as I know. – Gray Jul 24 '13 at 19:01
  • I am assuming this is for a winforms project? – JBelter Jul 24 '13 at 19:08
  • Yes, it is a winforms application. – jgetrost Jul 24 '13 at 19:11
  • This may help, although I am not sure what class/method you could pass the enumeration into: [MSDN Keys Enumeration](http://msdn.microsoft.com/en-us/library/system.windows.forms.keys%28v=vs.100%29.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1) – JBelter Jul 24 '13 at 19:15
  • @jgetrost Sorry, you are of course right to ask for some explanation there. Basically, my problem is I don't know vb.net very well. I found this answer here for c#: http://stackoverflow.com/questions/7181978/special-keys-on-keyboards . Maybe someone who knows VB.net (or you) can convert this to be usable. – Gray Jul 24 '13 at 19:24
  • @Jbelter It looks like that method is to determine which special keys have been pressed, I don't care what keys the user presses, I want my application to simulate a key press to control media applications. – jgetrost Jul 24 '13 at 19:26
  • @jgetrost I've actually used the Keys enumeration to specify keys to press, however it was using a method that is confidential to my employer. So it can be done. **How** it can be done is still unknown. – JBelter Jul 24 '13 at 19:31
  • @Gray, the question you linked was regarding C#, but the answer produced was (mistakenly?) c++ code, can it be transtlated/converted? – jgetrost Jul 24 '13 at 19:34
  • @jgetrost his wording was confusing, but the code he posted was c#, not c++. He was referring to the link he sent. In any case, I ported it to VB; let me know if you need any help/explanation. – Gray Jul 24 '13 at 19:41

1 Answers1

3

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

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

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

Community
  • 1
  • 1
Gray
  • 7,050
  • 2
  • 29
  • 52