C# - Hide window on input from user [duplicate] - c#

This question already has answers here:
Best way to implement keyboard shortcuts in a Windows Forms application?
(9 answers)
Closed 7 years ago.
I have put this code in my application, I want the window to hide when control + Q is clicked, why does this not work?
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.Q)
{
this.Hide();
}
}

You can check the Key modifier in order to handle Keys combination
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Q && e.Modifiers == Keys.Control)
{
//do stuff
}
}

Related

UWP Ctrl+F implementation

I'm creating an UWP app (C# .NET) where there is textbox. I want to implement a shortcut (Ctrl+F) to search texts in the textbox. I know how to find texts, but I don't know how to implement the shortcut.
I found this:
if ((e.Control && e.KeyCode == Keys.F) || (e.Control && e.KeyCode == Keys.S))
{
//do something
}
...but it isn't working for UWP. I tried this (textarea is name of textbox):
private void textarea_KeyDown(object sender, KeyRoutedEventArgs e)
{
if ((e.Key == Windows.System.VirtualKey.Control) && (e.Key == Windows.System.VirtualKey.F))
{
flayoutFind.ShowAt(appBarButtonFind as FrameworkElement);
}
}
but it isn't working too. How can I do it?
And for the future, is there any way, how to override default functionality and shortcut of textbox Ctrl+Z (undo)?
You should be using "Accelerators" and "Access keys" as described here:
https://learn.microsoft.com/en-us/windows/uwp/input-and-devices/keyboard-interactions
Basically, you will have to register for events
Window.Current.CoreWindow.Dispatcher.AcceleratorKeyActivated += Dispatcher_AcceleratorKeyActivated;
private void Dispatcher_AcceleratorKeyActivated(CoreDispatcher sender, AcceleratorKeyEventArgs args)
{
//Implementation
}
You can check the sample in detail here: https://github.com/Microsoft/DesktopBridgeToUWP-Samples/blob/master/Samples/SQLServer/BuildDemo/MainPage.xaml.cs

windows form keyup event is not working c# [duplicate]

This question already has an answer here:
keydown in c# doesn't work for some reason
(1 answer)
Closed 5 years ago.
This code is not working in my main form
void main_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter key pressed");
e.SuppressKeyPress = true;
}
}
Try to set Form property: main.KeyPreview = true;

How to perform action when Shift+Enter is pressed?

I have a TextBox in a C#/XAML desktop app and I want to detect the Shift+Enter command. How can I do this?
So far I have only been able to find information on commands like Ctrl+A, etc.
ModifierKeys.Shift allows you to identify key pressed combinations which includes Shift:
private void HandleKeyDownEvent(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter && (Keyboard.Modifiers == ModifierKeys.Shift))
{
// Handle..
}
}
Another option is Keyboard.IsKeyDown static method (see Shoe's answer).
if (Keyboard.Modifiers == ModifierKeys.Shift && Keyboard.IsKeyDown(Key.Enter))
{
MessageBox.Show("test");
}
A good example can be found here.
public void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
{
MessageBox.Show("Pressed " + Keys.Shift);
}
}

An error in key Down function [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I am working with c# windows application and i need to enter some record to database after inserting in to textbox. I have tried this code
private void textBoxItemCode_KeyDown(object sender, EventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("You have entered the correct key.");
}
}
but i am getting an error near e.KeyCode so how to make it
You need to use the following args
System.Windows.Forms.KeyEventArgs
Your handler should look like this
private void textBoxItemCode_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("You have entered the correct key.");
}
}
That way you have access to the KeyCode property which does not exist on the base EventArgs
It Should be like this :-
private void textBoxItemCode_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
MessageBox.Show("You have entered the correct key.");
}
}

C# - preparing event after pushing two buttons on keyboard [duplicate]

This question already has answers here:
How to use multiple modifier keys in C#
(9 answers)
Closed 8 years ago.
i've got problem. If I push one key I can get event for example:
if (e.KeyCode == Keys.F4)
{
Method();
}
How could I do the same if I push two keys? For example Enter + F4?
FormLoad()
{
this.KeyPreview = true;
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
}
void Form1_KeyDown(object sender, KeyEventArgs e)
{
//Works for Ctrl+F4
if (e.Control && e.KeyCode == Keys.F4)
{
//Do something
}
}
See if this work for you.

Categories