[C/JScript] Send VK_MEDIA_PLAY_PAUSE or CTRL+ALT+HOME

Discussion in 'Scripting' started by ofernandofilo, Jun 19, 2016.

  1. ofernandofilo

    ofernandofilo MDL Member

    Sep 26, 2015
    211
    128
    10
    #1 ofernandofilo, Jun 19, 2016
    Last edited by a moderator: Apr 20, 2017
    Disclaimer: I am not a programmer. I'm just a helper in the IT of my company. Beware of code I write!

    In my work, I had to integrate a Philips pedal with Windows Media Player in the background. Philips driver sends keystrokes to the active window but not to background windows.

    The integration is not yet complete, I just did tests on my computer, but I believe that I have an available basic solution.

    Let's show the codes.


    main.c, send VK_MEDIA_PLAY_PAUSE with SendInput
    Code:
    /**
     *  SendInput VKMP or VKBB keys
     *
     *  Please use: http://wmpkeys.sourceforge.net/ // Make "Fast Backward" use VK_BROWSER_BACK
     *
     *  @Author:  Fernando Di Ramos
     *     Here is where I hide: https://ofernandofilo.blogspot.com.br/
     *  @License: WTFPL Version 2
     *     SEE: https://en.wikipedia.org/wiki/WTFPL
     *
     *  This software was tested only with:
     *     Windows 7 Home Basic, Service Pack 1, 7601, x64, pt_BR [not free]
     *     Windows Media Player 12.0.7601.19148 [not free]
     *     WMP Keys 1.2.0.0 [free]
     */
    #define WINVER 0x0601
    // SEE: https://msdn.microsoft.com/en-us/library/aa383745%28VS.85%29.aspx
    // SEE: https://msdn.microsoft.com/en-us/library/windows/hardware/ff554695%28v=vs.85%29.aspx
    #define VK_BROWSER_BACK     0xA6
    #define VK_MEDIA_PLAY_PAUSE 0xB3
    // SEE: https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731%28v=vs.85%29.aspx
    #define WSCAN_BROWSER_BACK     0x6A
    #define WSCAN_MEDIA_PLAY_PAUSE 0x22
    // SEE: https://support.microsoft.com/en-us/kb/274333
    // SEE: http://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/scancode.doc
    #define KEYEVENTF_KEYDOWN   0x0000
    #define KEYEVENTF_KEYUP     0x0002
    // SEE: https://msdn.microsoft.com/en-us/library/windows/desktop/ms646271%28v=vs.85%29.aspx
    // SEE: https://msdn.microsoft.com/en-us/library/windows/desktop/ms646280%28v=vs.85%29.aspx
    // SEE: https://msdn.microsoft.com/en-us/library/windows/desktop/ms646281%28v=vs.85%29.aspx
    #define nInputs           1 // # of inputs
    #define INPUT_MOUSE       0
    #define INPUT_KEYBOARD    1
    #define INPUT_HARDWARE    2
    // SEE: https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx
    
    /**
     * Keyboard Input
     * https://msdn.microsoft.com/en-us/library/windows/desktop/ms646267%28v=vs.85%29.aspx
     * https://msdn.microsoft.com/en-us/library/windows/desktop/ms646268%28v=vs.85%29.aspx
     *
     * Scancodes Sets
     * https://en.wikipedia.org/wiki/Scancode#Scancode_sets
     * https://www.win.tue.nl/~aeb/linux/kbd/scancodes.html
     * https://www.win.tue.nl/~aeb/linux/kbd/scancodes-10.html
     * http://www.quadibloc.com/comp/scan.htm
     *
     */
    
    #include <windows.h>
    
    int main() {
       /**
        * You must define a KEYBDINPUT
        * SEE: https://msdn.microsoft.com/en-us/library/windows/desktop/ms646271%28v=vs.85%29.aspx
        */
       KEYBDINPUT keybdinput;
       keybdinput.wVk = VK_MEDIA_PLAY_PAUSE;
       keybdinput.wScan = WSCAN_MEDIA_PLAY_PAUSE;
       keybdinput.dwFlags = KEYEVENTF_KEYDOWN;
       keybdinput.time = 0;
       keybdinput.dwExtraInfo = 0;
    
       /**
        * You must define a LPINPUT/INPUT, which uses KEYBDINPUT
        * SEE: https://msdn.microsoft.com/en-us/library/windows/desktop/ms646270%28v=vs.85%29.aspx
        */
       LPINPUT lpinput;
       INPUT input;
       input.type = INPUT_KEYBOARD;
       input.ki = keybdinput;
       lpinput = &input;
    
       /**
        * SendInput function
        * SEE:https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx
        */
       // SendInput(UINT nInputs,LPINPUT pInputs,int cbSize);
       // Press down
       // SendInput(nInputs,lpinput,sizeof(input));
    
       // Release key
       keybdinput.dwFlags = KEYEVENTF_KEYUP;
       SendInput(nInputs,lpinput,sizeof(input));
    
       return 0;
    }
    
    /**
     *  Still confuse?
     *     SEE: https://batchloaf.wordpress.com/2012/04/17/simulating-a-keystroke-in-win32-c-or-c-using-sendinput/
     *     SEE: https://batchloaf.wordpress.com/2012/10/18/simulating-a-ctrl-v-keystroke-in-win32-c-or-c-using-sendinput/
     */
    
    sendkeystowmpkeys.js, send CTRL + ALT + HOME, with SendKeys [please, install WMP Keys to work]
    Code:
    /**
     * WINDOWS SCRIPT JSCRIPT
     *
     * CALL ME:
     *   wscript.exe sendkeystowmpkeys.js
     */
     
    var sendtowindows = WScript.CreateObject("WScript.Shell");
    // SEND: CTRL + ALT + HOME
    sendtowindows.SendKeys("^%{HOME}");
    
    WMP Keys: http ://wmpkeys.sourceforge .net/

    Tested in: Windows 7 Home Basic, SP1, 64 bits, pt_BR.
    Tested with: Code::Blocks + TDM-GCC (codeblocks-16.01mingw-setup.exe) and -static option in linker.
    (Settings > Compiler... > Linker settings > Other linker options: -static)

    cheers!
     

    Attached Files:

    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. Michaela Joy

    Michaela Joy MDL Crazy Lady

    Jul 26, 2012
    3,438
    3,578
    120
    Very nice!

    I use Code::Blocks from time to time. It's a great IDE.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...