running cmd application minimized not work - c#

I have a cmd application when i pressed insert while aplication is maximized run normal
but when application is minimized not work
What could I do
please help me
using System;
using System.Runtime.InteropServices;
class MainClass
{
static void Main()
{
ConsoleKeyInfo keypress;
keypress = Console.ReadKey(); // read keystrokes
if (keypress.Key == ConsoleKey.Insert)
{
Console.Write("One ");
}
}
}
not recognized key insert while is minimized

I think, you are looking for Global Hooks.

How can it work when the command line is minimized? It doesn't have any focus and your key strokes aren't registered.
The same can be said for almost any program - try it with notepad for instance
Edit: if you want to register to global key events, use glob hooks like TcKs suggested

Related

Launch application minimized if launched by system

I want to launch an application minimized or not depending on it was launched by system on startup (Windows) or not minimized if it was launched by user (double clicked on it).
I've made a converter program so far which will open in windows start up. I've used this to success.
RegistryKey rk = Registry.CurrentUser.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rk.SetValue("MyApplicationName", Application.ExecutablePath);
rk.Close();
rk.Dispose();
I've only put these codes to startup. I don't check if it's already in regedit because its not adding if it's already there. After I've set these codes to my program I've restart my computer and my program came at startup but it come in center of my screen like normal startup. Can I check if my program is started by windows on startup and set it to startup? Normally my program hides itself to system tray if I click "x" on top right corner. I've to right click->exit to actually close my program.
My question is: Is there any way to check how was launched by the system (Windows) or by the user?
As they are both started with the same user it is going to be hard to detect. As an alternative, you could have a parameter in your application telling it to start reduced or not. By default it opens the window and if the parameter is set you do not show it. You will just have to add this parameter in the execution command stored in the registry.
Here are some code:
Program.cs, you look if the -minimized argument is provided and you pass this information to your Form class
using System;
using System.Linq;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
static class Program
{
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1(args.Contains("-minimized")));
}
}
}
Form1.cs: if the boolean minimized is true, start minimized
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1(bool minimized)
{
InitializeComponent();
if (minimized)
{
WindowState = FormWindowState.Minimized;
}
}
}
}
Then you can start your program providing it the -minimized argument:
.\WindowsFormsApp1.exe -minimized
or not
.\WindowsFormsApp1.exe
If I understood correctly, you want to launch your application minimized, right?
In that case, you might need to add this code:
If you're developing a WinForms app
this.WindowState = FormWindowState.Minimized;
If you're developing a WPF app
this.WindowState = WindowState.Minimized;

Use Keyboard.IsKeyDown in C# console application

I'm writing an console application witch Displaying certain data on the console screen, than checking for user input from the keyboard and finally handleing it by need. all single threaded.
For that i tried using Keyboard.IsKeyDown Method from System.Windows.Input namespace. and visual studio wo'nt allow it.
Does anyone knows why and can help me?
I dont see other way implementing that logic using only one thread and no timer's.
Use Console.ReadKey() to read input from the keyboard in a console application.
Note that this is a blocking call. If you don't want to block, combine with Console.KeyAvailable. For example, this program will loop and display if a key is pressed every 10th of a second:
static void Main(string[] args)
{
do
{
if (Console.KeyAvailable)
{
var key = Console.ReadKey();
Console.WriteLine(key.Key);
}
else
{
Console.WriteLine("No key pressed");
}
System.Threading.Thread.Sleep(100);
} while (true);
}

Capture keyboard strokes with timing C# [duplicate]

I know there is a question for Windows Forms but it doesn't work in the console, or at least I couldn't get it to work. I need to capture key presses even though the console doesn't have focus.
You can create a global keyboard hook in a console application, too.
Here's complete, working code:
https://learn.microsoft.com/en-us/archive/blogs/toub/low-level-keyboard-hook-in-c
You create a console application, but must add a reference to System.Windows.Forms for this to work. There's no reason a console app can't reference that dll.
I just created a console app using this code and verified that it gets each key pressed, whether or not the console app has the focus.
EDIT
The main thread will run Application.Run() until the application exits, e.g. via a call to Application.Exit(). The simplest way to do other work is to start a new Task to perform that work. Here's a modified version of Main() from the linked code that does this
public static void Main()
{
var doWork = Task.Run(() =>
{
for (int i = 0; i < 20; i++)
{
Console.WriteLine(i);
Thread.Sleep(1000);
}
Application.Exit(); // Quick exit for demonstration only.
});
_hookID = SetHook(_proc);
Application.Run();
UnhookWindowsHookEx(_hookID);
}
NOTE
Possibly provide a means to exit the Console app, e.g. when a special key combo is pressed depending on your specific needs. In the

How to monitor focus changes?

Well Sometimes I am typing and very rarely it happens that something steals focus, I read some solution (even a VB watch) but they don't apply to me. Is there any windows-wide 'handle' which handles ANY focus changes?
It doesn't matter in which language, C, C++, VB.NET, C#, Anything .NET or windows related, Batch, PoweShell, VBS Script... As Long as I am able to monitor every focus change and log it into a file/cmd window/visual window.
Something like:
void event_OnWindowsFocusChange(int OldProcID, int NewProcID);
would be very usefull. Or maybe there are tools for this already (which I can't find?)
One way would be to use the windows UI Automation API. It exposes a global focus changed event. Here is a quick sample I came up with (in C#). Note, you need to add references to UIAutomationClient and UIAutomationTypes.
using System.Windows.Automation;
using System.Diagnostics;
namespace FocusChanged
{
class Program
{
static void Main(string[] args)
{
Automation.AddAutomationFocusChangedEventHandler(OnFocusChangedHandler);
Console.WriteLine("Monitoring... Hit enter to end.");
Console.ReadLine();
}
private static void OnFocusChangedHandler(object src, AutomationFocusChangedEventArgs args)
{
Console.WriteLine("Focus changed!");
AutomationElement element = src as AutomationElement;
if (element != null)
{
string name = element.Current.Name;
string id = element.Current.AutomationId;
int processId = element.Current.ProcessId;
using (Process process = Process.GetProcessById(processId))
{
Console.WriteLine(" Name: {0}, Id: {1}, Process: {2}", name, id, process.ProcessName);
}
}
}
}
}
You can monitor focus changes with a hook. SetWindowsHookEx(), using the WH_SHELL hook gets it done. The callback gets the HSHELL_WINDOWACTIVATED notification.
This isn't easy to get going, particularly in a managed language since it requires a DLL that can be injected. Nor could you reliably tell the difference between an intended focus change or a process shoved the window and stole the focus. Which Windows tries to prevent but there's a backdoor called AttachThreadInput() that fools that code.
It is never difficult to tell what process does this. After all, it tried to activate one of its windows. Uninstalling that program is the simple and best fix.

C# Prevent the Console window from displaying for half a second?

I am trying to create a super basic consol application why does the consol display for less then half a second and then exit?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyHelloWorldApplication
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Test");
}
}
}
You are doing nothing after the Console.Writeline() method so the app will close.
Adding a Console.ReadKey() will stop the app from closing until you have pressed a key.
If you are using VisualStudio try CTRL + F5 or just F5 key. One of them will do the trick.
F5 - Will let you to run the application with debugging enabled.
CTRL + F5 - Will run application with out debugging.
Or Try:
Console.ReadLine();
At the end of Main method which will let program run until Enter key is pressed.
I presume you're debugging, in which case it disappears because execution of your program has finished.
Add a Console.ReadLine(); call to the end of your main method and it won't exit until you hit the return key.
it starts, runs, writes "Test" then closes.
add
Console.ReadLine();
after your WriteLine("Test") and it'll wait for you to press ENTER before closing.
using System.Threading;
Thread.Sleep(500);//500 msec.

Categories