Is it possible to check pressed keys in a Console Application in any other way than Console.ReadKey()?
I've changed the project settings to Windows Application so that the console window won't show. But if the console window isn't shown the application crashes since Console.ReadKey() requires a console window. Is there any workaround?
NOTE:
I don't want any window open, just track a key-press, through a thread.
You cannot receive key pressed event if you do not have a window. You hae to have a console or "desktop" window.
In case of console 'Console.ReadKey()' does the job, in case of WPF or Windows Forms 'KeyPressed' event does the job.
Related
I created app that open every time that I start pc. So its so annoying to close it every time so I'm wondering if its some code that will hide my console app. I saw videos and tutorials on forms but idk how to do it with console app.
The easiest way to do this is change your console app to a windows app.
Console apps get a console made for them by Windows. But if you change it to a windows forms app, then windows expect the application to make a window, so if you never make a window, then it will never show
The other way is to turn your application into a service. This has some additional requirements in terms of programming
Option 1
You can use this run command:
start /min "SomeTitle" MyConsoleApp.exe myarg1 myarg2
Thus it will be on the taskbar minimized.
Option 2
If you use a file link in the start menu, select the start minimized option for the exe.
Option 3
Using a WinForm app you will be able to use a tray icon by setting the main form as not visible, to say it simply because it can be complex according to the expected behavior, and it will not be in the taskbar too.
Option 4
If you don't want a main form, create a win form app, delete the form file and the code in the main method, and you're done, without GUI nor console, no main input and no main output but you can show MessageBox and some forms when necessary, just a background process only visible in the Task Manager.
With that you can add a tray icon to to offer exit and some status information for example.
Option 5
Also you can create a windows service:
.NET console application as Windows service
Note
In all cases, if you don't use an internal message events dispatcher like the WinForms Application pattern or WPF and so on, be carefull to not saturate the CPU with the processings like with loops and use Thread.Sleep() between iterations or any thread idleing pattern or some timer if necessary.
I have written a code in c# that gets processes by name, activates the window of the desired process and sends some hotkeys to this process. The problem is that the desired process has two windows, a console window and a GUI window, as a result sometimes the GUI window is activated and everything works fine, but sometimes the console window is activated and nothing happens. So my question is, is there a way to distinguish between that process’ GUI and console window, so by program won’t get confused and only activate the GUI window ?
(By activating I mean bringing this process’ window to the foreground in order for the sending hotkeys to work. I have no access to the code of the desired process so I cannot change the way it starts up).
I'm working a small WPF based program for launching applications through system wide hotkeys implemented using hooking. I'm implementing it in C# and Visual Studio 2010.
When I detect the specific keypress I use Process.Start(...) to run the application. This works fine while Visual Studio is active, placing the new application in the foreground with input focus, as I would expect. If my launcher is in the background (behind another active program), it still detects the key and starts the application correctly, in front of everything else.
The problem is, that when I run the launcher without Visual Studio active, and my launcher application isn't front, neither will applications it starts. They appear in front of the launcher but behind the active application.
I can see that other software, like AutoHotkey, is able to do hotkey launching with this behavior, but I fail to see what I'm doing wrong.
Update: Just figured out a solution to this issue that works in my development environment. I first register a global hotkey through the Windows API RegisterHotKey using the launcher main window handle. The key could be any, but should be one that normally doesn't exist physically, F24 in my case. Then, whenever I detect a keypress through the hooks that should launch an app, I first call keybd_event to 'fake' a keypress for the hotkey.
For WPF use:
keybd_event((byte)KeyInterop.VirtualKeyFromKey(Key.F24),0,KEYEVENTF_KEYUP,0);
For WinForms use:
keybd_event((byte)Keys.F24,0,KEYEVENTF_KEYUP,0);
This will bring enough focus to the launcher, so that Process.Start(...) makes the executed program get in front. It does not bring the launcher window to front, nor does it make the launcher accept inputs.
If Activate() is called on the main window after the keybd_event(), this will bring the main window to front and allow for keyboard input, just as if the user had task switched.
I have a C++ - based application. This application triggers in turn a C# based WinForm executable. I use system() to launch this. When I do this, I get system (command prompt) window alongwith my form. My question is how to suppress this window. I want only to launch the application without prompt window...
// Trigger the form application
system("C:\\tmp\\MyApp\\SBCForm.exe");
i have a console application project. it has gui (winforms) and console window which is used as debug console (application's run log).
now, if i have got debug disabled i hide console window with ShowWindow native call.
the problem is: after i close application window by pressin the X button or by quitting it (Application.Exit()) i have a zombie console process.
if i close console on the very begining by Environment.Exit(0) the form is not been showed.
i have tried to modify forms Closed event by adding there Environment.Exit(0), but that did not work which means that there still remains a zombie console process.
how can i close console window on application exit?
If it is a console application, the console will not "close" until the process terminates. So you just simply need to close the form normally... assuming you "opened" it like you should.
To "open" the form, you need to run it. Run it by calling Application.Run(Form) (the same way you would initialize an actual form project). Since a console is allocated to your process, your form may write to Console.Out and you should see what you write.