Pressing a key will spawn a lot of screens - c#

I have the code below:
else if (keyboardState.IsKeyDown(Keys.E)) // if the 'E' key is pressed
{
form = new InventoryScreen(); //define winform instance
form.Show(); //display winform
}
It works, maybe too well. Because when the user presses and holds the 'E' key, the program generates a lot of winform screens. Is there a way to instruct the program to only run the following only once? Also, how would I write it so that if I press the 'E' key again, the window will disapear?

I would create a separate event handler firing on keyup. Within the handler you can check to see if the key is indeed 'E'. The keyup event will only fire once when the key is pressed and not fire while the key is held down.
See:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown(v=vs.110).aspx

XNA is not event-driven, it is instead a game loop.
There is no built-in event that fires on keys being press or not. One option is to only handle the 'E' event if no form is created.
A second option is to keep the old keyboard state, like this:
KeyboardState oldState;
Update(..):
KeyboardState now = Keyboard.GetState();
if (now.IsKeyUp(Keys.E) && oldState.IsKeyDown(Keys.E))
// i.e. a "release event"
{
form.Show();
}
oldState = now;
For the variant with disappearing, this might work or something similar, but you should probably instead handle it in the created form:
KeyboardState now = Keyboard.GetState();
if (now.IsKeyUp(Keys.E) && oldState.IsKeyDown(Keys.E))
// i.e. a "release event"
{
if (form.Visible)
{
form.Hide();
}
else
{
form.Show();
}
}
oldState = now;

Related

SendKeys.Send() does not send or react as I expect

I have a problem using Sendkeys.Send in my C# application and I really cannot understand why. When using it then it does not send what I expect to the active application. I am using it together with the global hotkey manager, https://github.com/thomaslevesque/NHotkey
I have created this simple PoC that, for my part at least, will be able to reproduce my problem. Just launch Wordpad and press the hotkey, ALT + O:
using System.Windows.Forms;
using System.Diagnostics;
using NHotkey.WindowsForms;
namespace WindowsFormsApp5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// Convert string to keys
string hotkey = "Alt + O";
KeysConverter cvt;
Keys key;
cvt = new KeysConverter();
key = (Keys)cvt.ConvertFrom(hotkey);
// Setup the hotkey
HotkeyManager.Current.AddOrReplace("MyID", key, HotkeyAction);
// Copy some text to the clipboard that I want to paste to the active application
Clipboard.SetText("My String");
}
private void HotkeyAction(object sender, NHotkey.HotkeyEventArgs e)
{
Debug.WriteLine("Pressed the hotkey");
SendKeys.Send("^v");
// SendKeys.Send("Test string");
e.Handled = true;
}
}
}
When I do this in Wordpad, then instead of pasting the clipboard (^v equals CTRL + V) then it tries to "Paste Special":
Even if I do the most simple thing and then just put some text in SendKeys.Send, then it seems to be messing with the menus in Wordpad? SendKeys.SendWait is not any different.
I have been trying to figure this out for quite some time now but I simply do not understand why it does that. Basically, I need to paste the clipboard on a hotkey though it doesn't need to be with this exact method so if anyone knows another way of doing it then I would appreciate some hints.
MY IMPLEMENTED SOLUTION
Based on the accepted answer then I did change my implementation slightly as I could not get it working with just a timer. I may have missed something(?) but this is working.
In basic then I change focus to my application as soon as the hotkey is detected, to avoid conflict with modifier keys (ALT etc) in the active application. I then create an invisible form and when I detect a KeyUp event, then I check for modifier keys and if none is pressed down then I enable a timer and immediately switch focus back to the originating application. After 50ms the clipboard will be pasted to the active application.
Something like this:
// Somewhere else in code but nice to know
// IntPtr activeApp = GetForegroundWindow(); // get HWnd for active application
// SetForegroundWindow(this.Handle); // switch to my application
private System.Timers.Timer timerPasteOnHotkey = new System.Timers.Timer();
// Main
public PasteOnHotkey()
{
InitializeComponent();
// Define the timer
timerPasteOnHotkey.Elapsed += new ElapsedEventHandler(OnTimedEvent);
timerPasteOnHotkey.Interval = 50;
timerPasteOnHotkey.Enabled = false;
// Make the form invisble
this.Size = new System.Drawing.Size(0, 0);
this.Opacity = 0.0;
}
private void PasteOnHotkey_KeyUp(object sender, KeyEventArgs e)
{
// Check if modifier keys are pressed
bool isShift = e.Shift;
bool isAlt = e.Alt;
bool isControl = e.Control;
// Proceed if no modifier keys are pressed down
if (!isShift && !isAlt && !isControl)
{
Hide();
// Start the timer and change focus to the active application
timerPasteOnHotkey.Enabled = true;
SetForegroundWindow(activeApp);
}
}
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
timerPasteOnHotkey.Enabled = false;
SendKeys.SendWait("^v"); // send "CTRL + v" (paste from clipboard)
}
When you use SendKeys.Send in a response to a key press then the keys you send may be combined with the physical keys you’re holding at that moment. In this case you’re holding Alt, so Wordpad assumes you pressed Alt-Ctrl-V instead of just Ctrl-V. Also Alt opens menu, so sending other keys may relate to hotkeys there.
Adding a delay will remove this issue, and usually when sending key presses it would be done as not relating to other key presses so it won’t be a problem.

Theaded Console.Getkey() seems to requires multiple key presses to register the key that is pressed

I am creating a console application that shows simple animations with text. I have a thread that waits for the user to press "Up Arrow", "Down Arrow" and "Enter/Carriage Return/Return". It seems that the user must press any of these keys multiple times for the key to be registered, however sometimes the key is registered instantly. I assume, as you would too; that this is a problem with my code. I had a power failure last week and it wiped out part of my project, whilst rewriting my code this problem appeared.
I have tried using
if(Console.KeyAvailable == true && Console.Readkey().key == [one of the three keys])
to no avail.
I tried removing (Memory memory) from the class leaving it just
private static void GetKeyboardInput() {}
but this didn't seem to have any effect. BTW, when I did that the thread was declared like this
Thread getkey = new Thread(GetKeyboardInput);
perhaps I am doing this part wrong?
Here is some of my code neutered for size...
// declare listener thread and start it.
Thread GetKey = new Thread(() => GetKeyboardInput(memory));
GetKey.Start();
while (GetKey.ThreadState == ThreadState.Running)
{ Animate(memory); } // this is the text animation, ---> Menuitem <---
// the arrows move in and out from the menu item
Animate(memory); runs in the current tread, so it isn't the culprit, correct?
// This is the code that the thread runs
// Memory class contains all stored values for this program
private static void GetKeyboardInput(Memory memory)
{
while (Console.ReadKey().Key != ConsoleKey.Enter)
{
if (Console.ReadKey().Key == ConsoleKey.UpArrow)
{
// move up. There is more code here but not relevant
}
if (Console.ReadKey().Key == ConsoleKey.DownArrow)
{
// Move Down. Same as before
}
}
Thread.CurrentThread.Abort();
}
I expect that in less than 1000ms after a key is pressed that the revelant code within the if statements gets executed and the screen will show a change to the user, if any.
Actual results are that sometimes the program reacts to user input instanly, and other times the user has to press the key multiple times to register a change. This is not session dependent either, at any time during execution both of these above problems will/can be present.
The problem is that you're calling Console.ReadKey() multiple times: once to check that it's not enter, again to check whether it's UpArrow, yet again to check whether it was DownArrow. You probably want to read it once, and store it in a variable.
// This is the code that the thread runs
// Memory class contains all stored values for this program
private static void GetKeyboardInput(Memory memory)
{
while (true)
{
var key = Console.ReadKey().Key;
if (key == ConsoleKey.Enter)
{
break;
}
else if (key == ConsoleKey.UpArrow)
{
// move up. There is more code here but not relevant
// meaning these values effect the animation and nothing else
}
else if (key == ConsoleKey.DownArrow)
{
// Move Down. Same as before
}
}
// No need to call Thread.Abort - exiting this method does the same
}

Suppressing global windows shortcuts while recording keypresses

Is it possible to suppress the windows global shortcuts while recording keypresses ?
I have a Windows Form application written in c#, and using this library to record keypresses to use later in macros. Now when I record the key combinations that are used by Windows (i.e. L Control + Win + Right Arrow to change virtual desktop on Win 10), I'd like my app to record it but avoid windows actually using it while I record which is quite annoying.
I have a checkbox to enable key capturing, on click event
m_KeyboardHookManager.KeyDown += HookManager_KeyDown;
the HookManager_KeyDown is simply like this
private void HookManager_KeyDown(object sender, KeyEventArgs e)
{
Log(string.Format("KeyDown \t\t {0}\n", e.KeyCode));
string [] sArr = new string [2];
if (keyBindingArea1.Text != "")
{
sArr[0] = keyBindingArea1.Text;
sArr[1] = string.Format("{0}", e.KeyCode);
keyBindingArea1.Text = string.Join("+", sArr);
}
else
{
keyBindingArea1.Text = string.Format("{0}", e.KeyCode);
}
}
which display the key combination in a comboText control. (This code is taken directly from the demo attached to the package.
Now the recording work well if for instance I press L Control + Win, then I release the keys and press the third one (i.e. Right Arrow), this will not trigger Windows shortcuts but it is quite annoying to have it work like that.
Appreciate any help. Thanks
Try to use e.Handled property of the event. If you set it to true it will terminate key procesing chain. As a rule oher applications in the processing chain will not get it. I am not sure it is going to work for such a low level windows staff as virtual desktop switch.
private void OnKeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
bool isThirdKey = //Some state evaluation to detect the third key was pressed
if (isThirdKey) e.Handled = true;
}

Maximize C# Application from System Tray using Keyboard Shortcut

Can I know if there is anyway that I can maximise my windows form application from the system tray using say a Keyboard Shortcut rather than clicking on it?
I am currently minimizing using this piece of code
//Minimize to Tray with over-ride for short cut
private void MinimiseToTray(bool shortCutPressed)
{
notifyIcon.BalloonTipTitle = "Minimize to Tray App";
notifyIcon.BalloonTipText = "You have successfully minimized your app.";
if (FormWindowState.Minimized == this.WindowState || shortCutPressed)
{
notifyIcon.Visible = true;
notifyIcon.ShowBalloonTip(500);
this.Hide();
}
else if (FormWindowState.Normal == this.WindowState)
{
notifyIcon.Visible = false;
}
}
Hence, I need a keyboard shortcut that should maximize it. Much thanks!
EDIT: If you simply want to 'reserve a key combination' to perform something on your application, a Low-Level keyboard hook whereby you see every keypress going to any other application is not only an overkill, but bad practice and in my personal view likely to have people thinking that you're keylogging! Stick to a HOT-KEY!
Given that your icon will not have keyboard focus, you need to register a global keyboard hotkey.
Other similar questions:
How can I register a global hot key to say CTRL+SHIFT+(LETTER)
Best way to tackle global hotkey processing in c#?
Example from Global Hotkeys With .NET:
Hotkey hk = new Hotkey();
hk.KeyCode = Keys.1;
hk.Windows = true;
hk.Pressed += delegate { Console.WriteLine("Windows+1 pressed!"); };
if (!hk.GetCanRegister(myForm))
{
Console.WriteLine("Whoops, looks like attempts to register will fail " +
"or throw an exception, show error to user");
}
else
{
hk.Register(myForm);
}
// .. later, at some point
if (hk.Registered)
{
hk.Unregister();
}
To do this, you must use "Low-Level Hook".
You will find all information about it on this article : http://blogs.msdn.com/b/toub/archive/2006/05/03/589423.aspx
Look at this too : http://www.codeproject.com/Articles/6362/Global-System-Hooks-in-NET
I second Franck's suggestion about a global keyboard hook. Personally, I had very good experiences with the CodeProject article "Processing Global Mouse and Keyboard Hooks in C#".
As they write in their article, you can do things like:
private UserActivityHook _actHook;
private void MainFormLoad(object sender, System.EventArgs e)
{
_actHook = new UserActivityHook();
_actHook.KeyPress += new KeyPressEventHandler(MyKeyPress);
}
You could then call a function in your MyKeyPress handler that opens your window.
If you follow the guide here. It will show you how to register a global shortcut key.
public partial class Form1 : Form
{
KeyboardHook hook = new KeyboardHook();
public Form1()
{
InitializeComponent();
// register the event that is fired after the key press.
hook.KeyPressed += new EventHandler<KeyPressedEventArgs>(hook_KeyPressed);
// register the CONTROL + ALT + F12 combination as hot key.
// You can change this.
hook.RegisterHotKey(ModifierKeys.Control | ModifierKeys.Alt, Keys.F12);
}
private void hook_KeyPressed(object sender, KeyPressedEventArgs e)
{
// Trigger your function
MinimiseToTray(true);
}
private void MinimiseToTray(bool shortCutPressed)
{
// ... Your code
}
}

how to detect the (Shift+Delete) key press?

I'm making a custom Terminal in a C# winform. When the user presses a key in the TextBox that I'm using I have to make sure that it's a valid key. I need to prevent them from being able to delete stuff and do whatever they want. I managed to handle the backspace and delete and others via the KeyPress and KeyDown events. I had a problem with the user pressing shift + delete. It deletes stuff just like backspace. I don't know what the integer equivalent of it is. I made a C++ program to detect it, it was simple:
char c = _getch();
cout << (int)c << endl;
I got a -32. It didn't work in C#.
After so many tries to encircle its value, I found that it was 0.
It worked, at least for yesterday, but now it doesn't. I'm also able to delete stuff with the shift+delete.
Here's my key validation method:
private bool IsInvalidKey(char key)
{
bool condition1 = !IsAsciiKey(key) && !IsNavigatingKey(key); // by navigation I mean: left, right, end and home keys.
bool condition2 = (GetCaretPosition() == CaretPosition.OutsideCommandLine && !IsNavigatingKey(key));
bool condition3 = ((key == (char)Keys.Back) && GetCaretPosition() == CaretPosition.BeginningOfCommandLine);
return (condition1 || condition2 || condition3);
}
private bool IsAsciiKey(char key)
{
return ((int)key > 0 && (int)key <= 127); // char(0) should be shift+delete
}
How can I detect this key and where (in which event, KeyPress or KeyDown?)
I'm using a normal TextBox control, not a RichTextBox.
You could try something like this in the KeyDown event.
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Shift && e.KeyCode == Keys.Delete) e.Handled = true;
}
Rather than trying to look at each individual key through the use of the KeyDown event, what you really need to do is do all of your validation in the TextChanged event, and you need to validate the entire entry (not just the last character) every time anything changes. This is the only way to support everything that the user could possibly do. As an example, the user could use the mouse to cut or paste text in the textbox, and that doesn't trigger any key press events at all.
If you want, you can handle the key press event as well so that you can prevent invalid characters/key from changing the text in the first place (and causing a full validation) but that's basically like performing client side validation of a field on a webpage; it's nice to do, but since you can get around it you can't only use it.

Categories