C
C#2y ago
Ezlanding

.NET Program that runs in the background [Answered]

How would I make an .exe program in c# .NET that runs in the background (It will have a tray icon instead). It will be used for capturing certain hotkey presses
32 Replies
Auger
Auger2y ago
What does it do? And is the tray icon required (may significantly increase complexity)?
Ezlanding
Ezlanding2y ago
The program will help me copy and paste things easily. It will be set up to capture a set of hotkeys for example CMD+G+1-9 (I need help figuring out how to recognize the inputs) and for each number will store highlighted text (again something that I need help with) and will have an associated paste function. This project is very non-concrete and I’m still in the research phase so I may scrap it if this is too hard to do. Another idea would be when CMD+c is pressed, it use the next keystroke (1-9) to store the data copied from CMD+c. It would be much, much easier to just make it a vscode extension, but before I resort to that I would like to see if I could make something that works on other programs like VS too. The tray icon is not required at all and just something to easily kill the program. If it will make it too complex, I don’t have to include it
Auger
Auger2y ago
Ah, that actually clarifies it a good amount. I was initially thinking of a Worker Services app, but that no longer seems appropriate since those have some limitations You could make an invisible WPF app potentially, since that should let you interact with tray icons and the clipboard
Ezlanding
Ezlanding2y ago
That sounds like a good idea (especially because I’ve worked with wpf before). How would I have a WPF app the doesn’t have any content Itbwould also be nice if it runs in the background/doesn’t have a tab on my pc
Auger
Auger2y ago
For the tray icon you can use the Hardcodet.Wpf.TaskbarNotification nuget. That's what I use
Ezlanding
Ezlanding2y ago
👍
Auger
Auger2y ago
In the App xaml you can do something like this
Auger
Auger2y ago
Some of those tags may not be required. I did something similar for a notification popup app
Ezlanding
Ezlanding2y ago
Ok Thanks
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Jester
Jester2y ago
you can PInvoke the RegisterHotKey function i think that should work i see you said in another thread you didnt get it to work?
Angius
Angius2y ago
I just use Win + V ¯\_(ツ)_/¯
Ezlanding
Ezlanding2y ago
So I have copying selected text working and registering hotkeys with the RegisterHotKey function, but I would have to make 10 RegisterHotKey functions, so Is there a better way to do it? (maybe register if any of the ALT+0-9 keys pressed with a value signifying which one it is) like some way to get a general 0-9 key as the last param of RegisterHotKey and read it using HwndHook (((int)lParam >> 16) & 0xFFFF) @The Court Jester ^
Jester
Jester2y ago
@Ezlanding you could use global raw input, then you get all the key presses basically a keylogger but its a lot of work to the the interop for that yourself
Ezlanding
Ezlanding2y ago
Can you share a Microsoft docs page on it?
Jester
Jester2y ago
Raw Input Overview - Win32 apps
This topic discusses user-input from devices such as joysticks, touch screens, and microphones.
Ezlanding
Ezlanding2y ago
Thanks
Jester
Jester2y ago
@Ezlanding i can give you some c# code i wrote for this
Ezlanding
Ezlanding2y ago
That would be good Thanks
Jester
Jester2y ago
private static unsafe void RegisterForGlobalRawKeyboardInput(HWND target_window_handle)
{
const ushort HID_USAGE_PAGE_GENERIC = 1;
const ushort HID_USAGE_GENERIC_KEYBOARD = 6;
const int FALSE = 0;

var rawInputDevice = new RAWINPUTDEVICE
{
usUsagePage = HID_USAGE_PAGE_GENERIC,
usUsage = HID_USAGE_GENERIC_KEYBOARD,
dwFlags = RAWINPUTDEVICE_FLAGS.RIDEV_INPUTSINK | RAWINPUTDEVICE_FLAGS.RIDEV_NOLEGACY | RAWINPUTDEVICE_FLAGS.RIDEV_NOHOTKEYS,
hwndTarget = target_window_handle
};

if (RegisterRawInputDevices(&rawInputDevice, 1, (uint)sizeof(RAWINPUTDEVICE)).Value is FALSE) { throw ... }
}

private static unsafe void WriteRawInputToBuffer(RAWINPUT* out_raw_input, LPARAM l)
{
const uint ERROR = unchecked((uint)-1);

var rawInputHandle = (HRAWINPUT)l.Value;
var size = (uint)sizeof(RAWINPUT);
if (GetRawInputData(rawInputHandle, RAW_INPUT_DATA_COMMAND_FLAGS.RID_INPUT, out_raw_input, &size, (uint)sizeof(RAWINPUTHEADER)) is ERROR) { throw ... }
}
private static unsafe void RegisterForGlobalRawKeyboardInput(HWND target_window_handle)
{
const ushort HID_USAGE_PAGE_GENERIC = 1;
const ushort HID_USAGE_GENERIC_KEYBOARD = 6;
const int FALSE = 0;

var rawInputDevice = new RAWINPUTDEVICE
{
usUsagePage = HID_USAGE_PAGE_GENERIC,
usUsage = HID_USAGE_GENERIC_KEYBOARD,
dwFlags = RAWINPUTDEVICE_FLAGS.RIDEV_INPUTSINK | RAWINPUTDEVICE_FLAGS.RIDEV_NOLEGACY | RAWINPUTDEVICE_FLAGS.RIDEV_NOHOTKEYS,
hwndTarget = target_window_handle
};

if (RegisterRawInputDevices(&rawInputDevice, 1, (uint)sizeof(RAWINPUTDEVICE)).Value is FALSE) { throw ... }
}

private static unsafe void WriteRawInputToBuffer(RAWINPUT* out_raw_input, LPARAM l)
{
const uint ERROR = unchecked((uint)-1);

var rawInputHandle = (HRAWINPUT)l.Value;
var size = (uint)sizeof(RAWINPUT);
if (GetRawInputData(rawInputHandle, RAW_INPUT_DATA_COMMAND_FLAGS.RID_INPUT, out_raw_input, &size, (uint)sizeof(RAWINPUTHEADER)) is ERROR) { throw ... }
}
Ezlanding
Ezlanding2y ago
Thanks What’s the unsafe modifier?
Jester
Jester2y ago
im using pointers so the code is "unsafe"
Ezlanding
Ezlanding2y ago
K
Jester
Jester2y ago
RAWINPUT* has to point to a struct on the stack or a pinned / fixed array i use the CsWin32 nuget package to do the interop for me, or you could use TerraFX
Ezlanding
Ezlanding2y ago
So this writes the currently pressed to key to out_raw_input?
Jester
Jester2y ago
yes you call WriteRawInputToBuffer on the WM_INPUT event
Ezlanding
Ezlanding2y ago
👍 @The Court Jester I am getting some errors like The type or namespace name 'RAWINPUTHEADER' could not be found also how excacly do I use this function? will it block the character or only capture the input?
Jester
Jester2y ago
it doesnt block the keys from reaching where they are going i cant help much rn
Ezlanding
Ezlanding2y ago
that may be a little bit of a dealbreaker as it will overwrite the selected text is it ok to have 14 registerhotkey functions?
Jester
Jester2y ago
its not possible to block key input or at least not easely i dont think it matters
Ezlanding
Ezlanding2y ago
k then Ill use a whole bunch of registerhotkeys
Accord
Accord2y ago
✅ This post has been marked as answered!