I know this is a neverending story ;)
I am new to C# and writing a little tool for working with pictures. I'm trying to catch keystrokes in my tool.
I have done a lot of Google searching, but this is the only code for catching keystroked that works fine for me (more or less).
public partial class HomeUserControl : UserControl
{
public HomeUserControl(MainForm mainForm)
{
mainForm.KeyPreview = true;
InitializeComponent();
this.mainForm = mainForm;
labelVersion.Text = string.Format(Resources.LabelVersion, AppInfo.AppVersion);
}
// catch Keyboard
private bool _altModifierPressed = false;
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
_altModifierPressed = (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt));
if (_altModifierPressed && Keyboard.IsKeyDown(Key.N))
{
buttonNew_Click(this, EventArgs.Empty);
// Do not send event to focused control by returning true.
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
.....
}
My problem now is: I can't use ALT+N directly after starting the app. I have to minimize/maximize my app or switch to another app and then back to mine. Then it works, but not at firing up the app.
Ah, i forgot: I'm using Visual Studio 19, Windows Forms and Net Framework 4.8.
What did i miss? I hope someone can help me a little.
My english isn't the best but i hope you understand what i try to explain ;)
Greetings Zarrex