I want to perform a event for key press in console in c#
how can we manage it
bcz i want to make a pair of keys for open a dialog box.
i have used many ways but no one is running.
can any one knows about it
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Aditya ChauhanPosted Feb 26, 2015, 2:32 AM
bcz i m making a windows app and perform this task
in the program.cs
i know in the program one or more entry point possible
but in the windows form application this show the error
now i am explaining ...what i m doing..
i make a windows form application
i am performing some task on when my app start..
for this i written the code in the entry point in program.cs
the form is hide
and i want to open form without focus ..
for this i want to make a shortcut key ...
if we can find any other alternative for this ...
i will
i have use
Manish Kumar ChoudharyPosted Feb 26, 2015, 2:17 AM
class Program
{
static void Main(string[] args)
{
Thread show = new Thread(Temp.ShowForm);
show.Start();
Console.Read();
}
}
public static class Temp
{
public static void ShowForm()
{
ConsoleKeyInfo info = Console.ReadKey();
if (info.Key == ConsoleKey.Escape)
{
Console.WriteLine("Escape has been pressed.");
}
}
}
Both classes should be separate.
Manish Kumar ChoudharyPosted Feb 26, 2015, 1:48 AM
Aditya ChauhanPosted Feb 26, 2015, 1:41 AM
Console.ReadKey()
is not working bcz is this is used by another thread
and this is work in your machine bcz you are work with only one thread.
Manish Kumar ChoudharyPosted Feb 26, 2015, 1:39 AM
Manish Kumar ChoudharyPosted Feb 26, 2015, 12:53 AM
Aditya ChauhanPosted Feb 26, 2015, 12:37 AM
in the main ()
{
}
but this a error :Cannot read keys when either application does not have a console or when console input has been redirected from a file. Try Console.Read.
how can we solved i have tried many ways...but i cant find
Aditya ChauhanPosted Feb 26, 2015, 12:24 AM
i will try.
bcz i am also performing a another thread for gathering the key information.
Manish Kumar ChoudharyPosted Feb 26, 2015, 12:15 AM
Console.WriteLine("... Press escape, a, then control X");
// Call ReadKey method and store result in local variable.
// ... Then test the result for escape.
ConsoleKeyInfo info = Console.ReadKey();
if (info.Key == ConsoleKey.Escape)
{
Console.WriteLine("You pressed escape!");
}
// Call ReadKey again and test for the letter a.
info = Console.ReadKey();
if (info.KeyChar == 'a')
{
Console.WriteLine("You pressed a");
}
// Call ReadKey again and test for control-X.
// ... This implements a shortcut sequence.
info = Console.ReadKey();
if (info.Key == ConsoleKey.X &&
info.Modifiers == ConsoleModifiers.Control)
{
Console.WriteLine("You pressed control X");
}
Console.Read();
Aditya ChauhanPosted Feb 26, 2015, 12:11 AM
and any type
when we press ctrl + 1 then this function call
i will give some code which i used ok
i want to call this after performing a key combination
VulpesPosted Feb 25, 2015, 3:58 PM
However, by handling this event, it's possible to do anything you like in the eventhandler and then cancelling the closure of the application so that it continues normally.
So, as long as you don't mind the key combination being Ctrl+C or Ctrl-Break, you could pop up a dialog box within the eventhandler for this event. Here's a simple demo:
Incidentally, it's possible to distinguish between Ctrl+C and Ctrl+Break using args.SpecialKey. So you could allow one of these combinations to end the application as it normally would. However, a problem here is that there's no 'Break' keys on most laptop keyboards (though it can usually be simulated in some way such as Ctrt+Fn+F11) so it's not an ideal solution.
Manish Kumar ChoudharyPosted Feb 25, 2015, 8:09 AM
one way is just read the input and check that with your combination if condition is true execute the code. String key combination can be read using ReadLine function or you can convert that key to ASCII code and check you condition.