This question already has answers here:
How to programmatically set the system volume?
(7 answers)
Closed 3 years ago.
i searched like hours, now i ask in this Forum.
How can I control the System Volume Setting of Windows 10?
Which Libary I need?
I am using Visual Basic 2015 and wanna programm a Windows Universal App with C#.
The programm should be able to:
Set Systemvolume to x%
increase the Systemvolume by x
decrease the Systemvolume by x
get the current Systemvolume
I found a similar Question and Answer, but the Answer doesent work.
private void Mute()
{
SendMessageW(new WindowInteropHelper(this).Handle, WM_APPCOMMAND, new WindowInteropHelper(this).Handle,
(IntPtr)APPCOMMAND_VOLUME_MUTE);
}
it can't find "WindowInteropHelper". But I implement:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
class VolumeChanger
{
private const byte VK_VOLUME_MUTE = 0xAD;
private const byte VK_VOLUME_DOWN = 0xAE;
private const byte VK_VOLUME_UP = 0xAF;
private const UInt32 KEYEVENTF_EXTENDEDKEY = 0x0001;
private const UInt32 KEYEVENTF_KEYUP = 0x0002;
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, UInt32 dwFlags, UInt32 dwExtraInfo);
[DllImport("user32.dll")]
static extern Byte MapVirtualKey(UInt32 uCode, UInt32 uMapType);
public static void VolumeUp()
{
keybd_event(VK_VOLUME_UP, MapVirtualKey(VK_VOLUME_UP, 0), KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(VK_VOLUME_UP, MapVirtualKey(VK_VOLUME_UP, 0), KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
public static void VolumeDown()
{
keybd_event(VK_VOLUME_DOWN, MapVirtualKey(VK_VOLUME_DOWN, 0), KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(VK_VOLUME_DOWN, MapVirtualKey(VK_VOLUME_DOWN, 0), KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
public static void Mute()
{
keybd_event(VK_VOLUME_MUTE, MapVirtualKey(VK_VOLUME_MUTE, 0), KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(VK_VOLUME_MUTE, MapVirtualKey(VK_VOLUME_MUTE, 0), KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
}
Using this, you can mute, and increase or decrease Systemvolume by 2 degree.
I still searching a way to get the current Systemvolume.
You can't do that. Universal apps are sandboxed and can't make global modifications to the system. This includes the system volume.
I believe there is a way using nircmd.
First download nircmd and attach it to the project:
http://www.nirsoft.net/utils/nircmd.html
Then, call it via cmd:
Run Command Prompt Commands
The commands you want are specified in the nircmd website.
for instance, to change the volume to x% use:
realativePath/nircmd.exe setsysvolume x
Related
This question already has answers here:
keybd_event along with PostMessage win32 not working when Visual Studio has focus (or any application run as admin)
(2 answers)
Closed 6 years ago.
I'm making an app that will pause/play music on an event. I am not making my own music player, rather I'd like to use the universal pause/play that windows has System wide. Is there any way to do this?
You'll have to use the keybd_event windows API to do this.
using System.Runtime.InteropServices;
...
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags,
UIntPtr dwExtraInfo);
public const int KEYEVENTF_KEYUP = 0x0002;
public const byte VK_MEDIA_PLAY_PAUSE = 0xB3;
private void button_Click(object sender, RoutedEventArgs e)
{
// No flags indicate key down
keybd_event(VK_MEDIA_PLAY_PAUSE, 0, 0, UIntPtr.Zero);
keybd_event(VK_MEDIA_PLAY_PAUSE, 0, KEYEVENTF_KEYUP, UIntPtr.Zero);
}
For a full list of key codes, you can use this class:
https://github.com/johnkoerner/KeyboardListener/blob/master/KeyboardListener/Keycodes.cs
This question already has answers here:
How to send a WPF window to the back?
(2 answers)
Closed 9 years ago.
I am making an application which reacts as a windows startup program.
It shows different icons on it so that user can open only the allowed ones.
Now when user open 2 programs consecutively first opened program goes at the back of my wpf app.
As app is maximized and user cant minimize it so it becomes hidden.
In windows forms we can set a form send to back so that every program float over it but unluckily there is no such property in wpf.
How to do it?
Try this:
using System.Runtime.InteropServices;
const UInt32 SWP_NOSIZE = 0x0001;
const UInt32 SWP_NOMOVE = 0x0002;
const UInt32 SWP_NOACTIVATE = 0x0010;"
static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
static void ShowWindowBack(Window window)
{
var x= new WindowInteropHelper(window).Handle;
SetWindowPos(x, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE");
}
Is there some way that I can send multimedia control commands like next song, pause, play, vol up, etc. to the operating system?
Commands that are sent when pressing Fn + some mapped ..key.
I am making a remote control for PC and sending those commands is essential.
You can use keybd_event to simulate keys presses, you have to simulate key down and then key up in order to recognize correctly
[DllImport("user32.dll", SetLastError = true)]
public static extern void keybd_event(byte virtualKey, byte scanCode, uint flags, IntPtr extraInfo);
public const int VK_MEDIA_NEXT_TRACK = 0xB0;
public const int VK_MEDIA_PLAY_PAUSE = 0xB3;
public const int VK_MEDIA_PREV_TRACK = 0xB1;
public const int KEYEVENTF_EXTENDEDKEY = 0x0001; //Key down flag
public const int KEYEVENTF_KEYUP = 0x0002; //Key up flag
private void ButtonClick(object sender, EventArgs e)
keybd_event(VK_MEDIA_PREV_TRACK, 0, KEYEVENTF_EXTENDEDKEY, IntPtr.Zero);
keybd_event(VK_MEDIA_PREV_TRACK, 0, KEYEVENTF_KEYUP, IntPtr.Zero);
}`
Actually, the answer of dxramax gives me erratic behavior. I'm posting this answer that gives me consistent behavior, and also has some more details.
To send multimedia keys, including Play/Pause, NextTrack, PrevTrack, etc, you can use keybd_event:
public class Program
{
public const int KEYEVENTF_EXTENTEDKEY = 1;
public const int KEYEVENTF_KEYUP = 0;
public const int VK_MEDIA_NEXT_TRACK = 0xB0;
public const int VK_MEDIA_PLAY_PAUSE = 0xB3;
public const int VK_MEDIA_PREV_TRACK = 0xB1;
[DllImport("user32.dll")]
public static extern void keybd_event(byte virtualKey, byte scanCode, uint flags, IntPtr extraInfo);
public static void Main(string[] args)
{
keybd_event(VK_MEDIA_PLAY_PAUSE, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero); // Play/Pause
//keybd_event(VK_MEDIA_PREV_TRACK, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero); // PrevTrack
//keybd_event(VK_MEDIA_NEXT_TRACK, 0, KEYEVENTF_EXTENTEDKEY, IntPtr.Zero); // NextTrack
}
Here is a list to the supported key codes that this windows api can handle:
https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
The SendKeys class is very nice, but it's also limited. The approach above sends the key command directly to Windows OS.
Unfortunately, in most cases, keys Fn can't be sent using Windows API and as a result - using .NET classes. It depends on how the manufacturer has done this functionality. Probably it is supported by additional driver or even go over operation system.
You can check if it's possible to send Fn commands from the code by trying to hook them using Windows API code or some application like AutoHotKey. For instance, on my laptop, I can't hook multimedia commands.
Otherwise, if you are lucky, use SendKeys as mentioned in the comments.
If anyone wonders on this page like me. All posts above do not work, you also need bScan which is second parameter, you can get it with MapVirutalKey(VKCode,0).
Ex:
int keyValue = VK_MEDIA_NEXT_TRACK;
keybd_event(keyValue, MapVirtualKey(keyValue, 0), KEYEVENTF_KEYUP, 0);
keybd_event(keyValue, MapVirtualKey(keyValue, 0), KEYEVENTF_KEYUP, 0);
I need to send global keystrokes and mouse events to another application, which is coincidentally using using DirectX. (No controls/handles other than the window itself)
For example, I need to hold key X for 2 seconds and then release it...
I need to push Right Click down on coordinates x:600 and y:350, move the mouse 100 pixels down and then release the Right Click.
I also need to push 2 or more keys at once, like X and Y, and stop X after 2 seconds and Y after 2 more seconds.
So basically I would need full control of the input system...
It would also be ideal if I could control the application while maximized or in background. (optionally)
For the skeptics... The teacher made a DirectX application for drawing for our school. I am asked to make an application that draws samples on it, like a train or flower or something... I will be reading images and use the input to set the color and click on the canvas...
There are some possibilities. You may have a look at System.Windows.Forms.SendKeys and you can pInvoke some Win32 functions like SetForegroundWindow(), LockSetForegroundWindow() from gdi32.dll or from user32.dll SetCursorPos() and mouse_event to perform clicks:
Here a snippet for the Mouse events I used a while ago.
/**
* Mouse functions
*/
[DllImport("user32.dll", ExactSpelling=true)]
public static extern long mouse_event(Int32 dwFlags, Int32 dx, Int32 dy, Int32 cButtons, Int32 dwExtraInfo);
[DllImport("user32.dll", ExactSpelling=true)]
public static extern void SetCursorPos(Int32 x, Int32 y);
public const Int32 MOUSEEVENTF_ABSOLUTE = 0x8000;
public const Int32 MOUSEEVENTF_LEFTDOWN = 0x0002;
public const Int32 MOUSEEVENTF_LEFTUP = 0x0004;
public const Int32 MOUSEEVENTF_MIDDLEDOWN = 0x0020;
public const Int32 MOUSEEVENTF_MIDDLEUP = 0x0040;
public const Int32 MOUSEEVENTF_MOVE = 0x0001;
public const Int32 MOUSEEVENTF_RIGHTDOWN = 0x0008;
public const Int32 MOUSEEVENTF_RIGHTUP = 0x0010;
public static void PerformLeftKlick(Int32 x, Int32 y)
{
SetCursorPos(x, y);
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
Hope that pushes you in the right direction. A good resource is http://pinvoke.net/
If you want to use a library for C# that will make your work easier then read following link -
http://www.codeproject.com/Articles/117657/InputManager-library-Track-user-input-and-simulate
Other than .Net C# you can use other language alternative like in Java where, there is no confusion of direct x or normal input -
http://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I need to wake up a PC from sleep to perform some actions using C#.
I've used CreateWaitableTimer functions, everything goes fine. At given time the PC wakes up but the monitor stays in power save mode (turned off)!
So I want to know, how to turn the monitor ON after wake up?
PS I've tried "Complete Guide on How To Turn A Monitor On/Off/Standby" - with SendMessage (Codeproject) and SetThreadExecutionState(ES_DISPLAY_REQUIRED) - it doesn't work for me.
Any ideas?
Try making the mouse move. When i wake up my Windows 7 system with a tap on the keyboard the screen stays black until i move the mouse.
Cursor.Position = new Point(x, y);
for me it works fine to use pinvoke to call SendMessage.
code example for csharp:
using System;
using System.Runtime.InteropServices;
namespace MyDummyNamespace
{
class MyProgram
{
private static int Main(string[] args)
{
// your program code here
// ...
NativeMethods.MonitorOff();
System.Threading.Thread.Sleep(5000);
NativeMethods.MonitorOn();
return 0;
}
private static class NativeMethods
{
internal static void MonitorOn()
{
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (IntPtr)MONITOR_ON);
}
internal static void MonitorOff()
{
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (IntPtr)MONITOR_OFF);
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
private static int MONITOR_ON = -1;
private static int MONITOR_OFF = 2;
private static int MONITOR_STANBY = 1;
private static IntPtr HWND_BROADCAST = new IntPtr(0xffff);
private static UInt32 WM_SYSCOMMAND = 0x0112;
private static IntPtr SC_MONITORPOWER = new IntPtr(0xF170);
}
}
}
the above solution was inspired by this answer: https://stackoverflow.com/a/332733/1468842