C
C#3y 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
Auger3y ago
What does it do? And is the tray icon required (may significantly increase complexity)?
Ezlanding
EzlandingOP3y 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
Auger3y 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
EzlandingOP3y 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
Auger3y ago
For the tray icon you can use the Hardcodet.Wpf.TaskbarNotification nuget. That's what I use
Ezlanding
EzlandingOP3y ago
👍
Auger
Auger3y ago
In the App xaml you can do something like this
Auger
Auger3y ago
Some of those tags may not be required. I did something similar for a notification popup app
Ezlanding
EzlandingOP3y ago
Ok Thanks
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Jester
Jester3y 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
Angius3y ago
I just use Win + V ¯\_(ツ)_/¯
Ezlanding
EzlandingOP3y 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
Jester3y 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
EzlandingOP3y ago
Can you share a Microsoft docs page on it?
Jester
Jester3y ago
Raw Input Overview - Win32 apps
This topic discusses user-input from devices such as joysticks, touch screens, and microphones.
Ezlanding
EzlandingOP3y ago
Thanks
Jester
Jester3y ago
@Ezlanding i can give you some c# code i wrote for this
Ezlanding
EzlandingOP3y ago
That would be good Thanks
Jester
Jester3y 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
EzlandingOP3y ago
Thanks What’s the unsafe modifier?
Jester
Jester3y ago
im using pointers so the code is "unsafe"
Ezlanding
EzlandingOP3y ago
K
Jester
Jester3y 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
EzlandingOP3y ago
So this writes the currently pressed to key to out_raw_input?
Jester
Jester3y ago
yes you call WriteRawInputToBuffer on the WM_INPUT event
Ezlanding
EzlandingOP3y 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
Jester3y ago
it doesnt block the keys from reaching where they are going i cant help much rn
Ezlanding
EzlandingOP3y 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
Jester3y ago
its not possible to block key input or at least not easely i dont think it matters
Ezlanding
EzlandingOP3y ago
k then Ill use a whole bunch of registerhotkeys
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server