❔ media key press

I want to press media key (music) using c# (next track, previous track, pause resume)A
23 Replies
circles.png
circles.png17mo ago
that is probably done with a windows api
circles.png
circles.png17mo ago
Manual control of the System Media Transport Controls - UWP applica...
The SystemMediaTransportControls class enables your app to use the system media transport controls that are built into Windows and to update the metadata that the controls display about the media your app is currently playing.
어부지망생
어부지망생17mo ago
I Already tried them but it doesnt work can u show me example??
circles.png
circles.png17mo ago
no i dont develop windowd
sibber
sibber17mo ago
you could use SendInput and send virtual media keys
sibber
sibber17mo ago
sibber
sibber17mo ago
Virtual-Key Codes (Winuser.h) - Win32 apps
The following table shows the symbolic constant names, hexadecimal values, and mouse or keyboard equivalents for the virtual-key codes used by the system. The codes are listed in numeric order.
어부지망생
어부지망생17mo ago
Thanks for helping me 🙂 How can i use this code? I looked your link and i found one "keybd_event( VK_NUMLOCK, 0x45, " like this?? Ah ok 😦 thanks for helping me
sibber
sibber17mo ago
no use SendInput, docs say to use it over keybd_input
어부지망생
어부지망생17mo ago
Im sorry but, could you show me example? I cant find one in here
sibber
sibber17mo ago
you pinvoke it have you ever done interop? ill write an example when i get to my pc
어부지망생
어부지망생17mo ago
Ok thanks for helping me 😉
sibber
sibber17mo ago
@어부지망생 i used CsWin32 to source gen the pinvoke definitions and stuff
using System.Runtime.InteropServices;
using Windows.Win32;
using Windows.Win32.UI.Input.KeyboardAndMouse;

var input = new INPUT[2];

input[0].type = INPUT_TYPE.INPUT_KEYBOARD;
input[0].Anonymous.ki.wVk = VIRTUAL_KEY.VK_MEDIA_PLAY_PAUSE;

input[1].type = INPUT_TYPE.INPUT_KEYBOARD;
input[1].Anonymous.ki.wVk = VIRTUAL_KEY.VK_MEDIA_PLAY_PAUSE;
input[1].Anonymous.ki.dwFlags = KEYBD_EVENT_FLAGS.KEYEVENTF_KEYUP;

PInvoke.SendInput(input, Marshal.SizeOf<INPUT>());
using System.Runtime.InteropServices;
using Windows.Win32;
using Windows.Win32.UI.Input.KeyboardAndMouse;

var input = new INPUT[2];

input[0].type = INPUT_TYPE.INPUT_KEYBOARD;
input[0].Anonymous.ki.wVk = VIRTUAL_KEY.VK_MEDIA_PLAY_PAUSE;

input[1].type = INPUT_TYPE.INPUT_KEYBOARD;
input[1].Anonymous.ki.wVk = VIRTUAL_KEY.VK_MEDIA_PLAY_PAUSE;
input[1].Anonymous.ki.dwFlags = KEYBD_EVENT_FLAGS.KEYEVENTF_KEYUP;

PInvoke.SendInput(input, Marshal.SizeOf<INPUT>());
어부지망생
어부지망생17mo ago
I dont know pinvoke and interop 😦 what is it?? I'll search for it
using System.Diagnostics;
using System.Net.Sockets;
using System.Net;
using System.IO;
using AudioSwitcher.AudioApi.CoreAudio;
using System;
using System.Windows.Media;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Windows.Input;
using Slack.Webhooks.Blocks;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Diagnostics;
using System.Net.Sockets;
using System.Net;
using System.IO;
using AudioSwitcher.AudioApi.CoreAudio;
using System;
using System.Windows.Media;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Windows.Input;
using Slack.Webhooks.Blocks;
using System.Runtime.InteropServices;
using Microsoft.Win32;
var input = new Input[2];

input[0].type = INPUT_TYPE.INPUT_KEYBOARD;
input[0].Anonymous.ki.wVk = VIRTUAL_KEY.VK_MEDIA_PLAY_PAUSE;

input[1].type = INPUT_TYPE.INPUT_KEYBOARD;
input[1].Anonymous.ki.wVk = VIRTUAL_KEY.VK_MEDIA_PLAY_PAUSE;
input[1].Anonymous.ki.dwFlags = KEYBD_EVENT_FLAGS.KEYEVENTF_KEYUP;

PInvoke.SendInput(input, Marshal.SizeOf<INPUT>());
var input = new Input[2];

input[0].type = INPUT_TYPE.INPUT_KEYBOARD;
input[0].Anonymous.ki.wVk = VIRTUAL_KEY.VK_MEDIA_PLAY_PAUSE;

input[1].type = INPUT_TYPE.INPUT_KEYBOARD;
input[1].Anonymous.ki.wVk = VIRTUAL_KEY.VK_MEDIA_PLAY_PAUSE;
input[1].Anonymous.ki.dwFlags = KEYBD_EVENT_FLAGS.KEYEVENTF_KEYUP;

PInvoke.SendInput(input, Marshal.SizeOf<INPUT>());
어부지망생
어부지망생17mo ago
어부지망생
어부지망생17mo ago
red line in here i have to install package?
sibber
sibber17mo ago
i used CsWin32
that's a package
sibber
sibber17mo ago
GitHub
GitHub - microsoft/CsWin32: A source generator to add a user-define...
A source generator to add a user-defined set of Win32 P/Invoke methods and supporting types to a C# project. - GitHub - microsoft/CsWin32: A source generator to add a user-defined set of Win32 P/In...
어부지망생
어부지망생17mo ago
i installed cswin32 but still red line so i created navtivemethods.txt but noting changed
sibber
sibber17mo ago
you need to put the names of the methods you want to use in it in this case SendInput
어부지망생
어부지망생17mo ago
var input = new INPUT[2];

input[0].type = INPUT_TYPE.INPUT_KEYBOARD;
input[0].Anonymous.ki.wVk = VIRTUAL_KEY.VK_MEDIA_NEXT_TRACK;

input[1].type = INPUT_TYPE.INPUT_KEYBOARD;
input[1].Anonymous.ki.wVk = VIRTUAL_KEY.VK_MEDIA_NEXT_TRACK;
input[1].Anonymous.ki.dwFlags = KEYBD_EVENT_FLAGS.KEYEVENTF_KEYUP;

PInvoke.SendInput(input, Marshal.SizeOf<INPUT>());
var input = new INPUT[2];

input[0].type = INPUT_TYPE.INPUT_KEYBOARD;
input[0].Anonymous.ki.wVk = VIRTUAL_KEY.VK_MEDIA_NEXT_TRACK;

input[1].type = INPUT_TYPE.INPUT_KEYBOARD;
input[1].Anonymous.ki.wVk = VIRTUAL_KEY.VK_MEDIA_NEXT_TRACK;
input[1].Anonymous.ki.dwFlags = KEYBD_EVENT_FLAGS.KEYEVENTF_KEYUP;

PInvoke.SendInput(input, Marshal.SizeOf<INPUT>());
this one doesn't work i installed package and no error but why 😦 sry for bothering you
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.