C# KeyPreview forma tıklamadan olay gerçekleşmeme

Katılım
22 Temmuz 2019
Mesajlar
1.839
Makaleler
2
Çözümler
23
Örneğin ESC tuşuna bastığımda formu kapatıyorum ama form arka planda kaldıysa ya da form dışında başka bir yere tıklarsam kapatma olayı gerçekleşmiyor.
Bunu nereye tıklarsam tıkalıyım kapanacak şeklide ayarlayabilir miyim?

[CODE lang="csharp" highlight="3"]private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Escape)
{
Application.Exit();
}
}
[/CODE]
Cevap:
Hotkey register etmek lazımmış.
Örnek çalışan kod:

[CODE lang="csharp" title="Eklenecek using"]using System.Runtime.InteropServices;[/CODE]

C#:
namespace FormDeneme.
{
 public partial class Form1 : Form.
 {
 [DllImport("user32.dll")]
 public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);

 public Form1()
 {
 InitializeComponent();
 int UniqueHotkeyId = 1;

 int HotKeyCode = (int)Keys.Escape;
 Boolean F9Registered = RegisterHotKey(this.Handle, UniqueHotkeyId, 0x0000, HotKeyCode);
 }

 protected override void WndProc(ref Message m)
 {
 if (m.Msg == 0x0312)
 {
 int id = m.WParam.ToInt32();
 if (id == 1)
 {
 Application.Exit();
 }
 }

 base.WndProc(ref m);
 }
 }
}
 
Son düzenleme:

Technopat Haberler

Geri
Yukarı