error cs1001 cant use command ctrl c - c#

Im trinying to write a word via pressed (ctrl+ c) but not work
please help me
please give me a hand
using System;
using System.Runtime.InteropServices;
class MainClass
{
static void Main()
{
ConsoleKeyInfo keypress;
keypress = Console.ReadKey(); // read keystrokes
if (keypress.Key == ConsoleKey.(CTRL-C))
{
Console.Write("One ");
}
}
}

Console applications consider CTRL+C to be the "termination command" of which is expected to cease functionality of the application and force it to close. Using CTRL+C as your input keys is faulty to begin with...
HOWEVER...
The keypress.Key should be the C key and you then need to also check if any "modifiers" are pressed. ALT, SHIFT, CTRL are the primary modifier keys and you usually check a boolean value to see if they are pressed or not . . so your application will check to see if the C key is pressed and if the CTRL modifier is pressed as well.
Check this out to get more information:
How to determine which key modifier is pressed

Related

running cmd application minimized not work

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

C# Console - Run Event on Key Press

I just wanted to ask if it was possible to make an event run if the player presses a specific button. This should be tested at every point in the console application but not if there is a Console.ReadLine() event used at the moment.
Is there a way to do this?
You can use Console.ReadKey() method like this
ConsoleKeyInfo keyInfo = Console.ReadKey();
if(keyInfo.Key == ConsoleKey.A)
{
}
For more info see https://msdn.microsoft.com/en-us/library/system.console.readkey(v=vs.110).aspx

trapping a key before it gets to the Console

THE SETTING:
A Console program, in .net
I am using readkey in a loop, which rewrites to the same line on the screen (Console.setCursorPosition)
however, if I type Enter, the Console pushes the text upwards, which is the usual command line behaviour.
THE QUESTION:
Is it possible to trap a key press (that Enter), so that my program will get the key, but the Console does not?
The environment is Linux, with Mono (and the program is supposed to be cross platform). so low level windows driver intercepting is not an option.
PS: I know the method of clearing and redrawing everything. I would like to know if my question is possible.
Thank you for any information
after modifying code found here https://stackoverflow.com/a/8898251/1951298 I came to something that may help you, you see that when user pushes Enter the cursor doesn't increment, and nothing is written into console
ConsoleKeyInfo keyinfo;
int left, top=0;
do
{
left = Console.CursorLeft;
top = Console.CursorTop;
keyinfo = Console.ReadKey(true);
if(keyinfo.Key.ToString().Equals("Enter"))
Console.SetCursorPosition(left,top-1);
else
Console.WriteLine(keyinfo.Key + " was pressed");
}
while (keyinfo.Key != ConsoleKey.X);
Console.ReadKey(true) should read the key but not show it.
You should intercept the key press. MSDN Documentation
var PressedKey = Console.ReadKey(true)

Disable normal behavior of Alt key

When pressing the Alt key, normally the focus goes to the window's menu. I need to disable it globally. Because my application works with Alt key. When the user presses some key in combination with the Alt key, my application sends another key to active application. So I just need to ignore Alt when it opens the menu.
I'm new to programming and I'm looking for VB.Net or C# code.
My first answer is to NOT use the Alt key for your program and use Ctrl instead. Blocking "normal" things from happening usually leads to pain in the future.
But if you must use the Alt key I would check out this article which uses message filters to try and intercept it at the application level.
If that doesn't do what you're looking for, you might need to look into Global hooks, this link will get you started down the path. Global hooks are generally considered evil so you should only use this if the above two suggestions don't work. You must make sure that you uninstall your hooks otherwise you might find that you need to reboot your computer often to fix weird keyboard problems.
This works for me:
private class AltKeyFilter : IMessageFilter
{
public bool PreFilterMessage(ref Message m)
{
return m.Msg == 0x0104 && ((int)m.LParam & 0x20000000) != 0;
}
}
And then you add the message filter like so:
Application.AddMessageFilter(new AltKeyFilter());
You can try something like this:
public void HandleKeyDown(object sender, keyEventArgs e)
{
//do whatever you want with or without Alt
if (e.Modifiers == Keys.Alt)
e.SuppressKeyPress = true;
}
This should allow you to use Alt for whatever you want but keep it from activating the menustrip. Note that e.SuppressKeyPress = true also sets e.Handled = true.
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.keyeventargs.suppresskeypress?view=windowsdesktop-5.0

Simulate key stroke in any application

How do I simulate a key stroke in a window that is not my C# application ?
Right now i'm using SendKeys.Send() but it does not work. The thing is I have a global keyboard hook so I catch the input directly from the keyboard and SendKeys.Send() is not seen like a real keyboard stroke.
The best would be to simulate a real keystroke this way, no matter what is the application i'm in, my program will catch it as if someone pressed a key.
I guess I found part of the problem. This is the event called if a key is pressed :
static void KeyBoardHook_KeyPressed(object sender, KeyPressedEventArgs e)
{
// Writes the pressed key in the console (it works)
Console.WriteLine(e.KeyCode.ToString());
// Check if pressed key is Up Arrow (it works and enters the condition)
if(e.KeyCode == Keys.Up)
{
// Send the key again. (does not work)
SendKeys.Send("{UP}");
}
}
I tried it this way to :
static void KeyBoardHook_KeyPressed(object sender, KeyPressedEventArgs e)
{
// Writes the pressed key in the console (it works)
Console.WriteLine(e.KeyCode.ToString());
// Check if pressed key is Up Arrow (it works and enters the condition)
if(e.KeyCode == Keys.Up)
{
// Send the key again. (does not work)
PostMessage(proc.MainWindowHandle,WM_KEYDOWN, VK_UP,0);
}
}
but it does not work either. The thing is since I send the key inside my event, will it call itself because a key has been pressed ? In case someone needs it, the code above.
[STAThread]
static void Main(string args)
{
KeyBoardHook.CreateHook();
KeyBoardHook.KeyPressed += KeyBoardHook_KeyPressed;
Application.Run();
KeyBoardHook.Dispose();
}
if you need the KeyBoardHook class I can post it too.
My guess is that my keyboard hook is catching the low-level keyboard outputs and the SendKeys is just simulating a keystroke so my hook doesn't catch it. Anybody thinks of a work around ?
I suggest you use this very cool library that masks all the complexity for you, the Windows Input Simulator available here: http://inputsimulator.codeplex.com/
I believe it's based on the Windows' SendInput function.
You can p/invoke the keybd_event (which is much simpler and easier) or SendInput (which is newer and has more capabilities) functions, which simulate keyboard input at a much lower level.

Categories