ESC キーで終了

ESC キーが押されるとプログラムを終了します。

前田稔(Maeda Minoru)の超初心者のプログラム入門

プロジェクトの設定

  1. 次のファイルを格納して下さい。
    ファイル名 説明
    EscKey.cs ESC キーで終了
  2. OnKeyDown() をオーバーライドして、キーの押し下げを検出します。
    Escape のときプログラムを終了します。
    マウスのクリックで終了確認 では Application.Exit() を使ったのですが、 今回は this.Dispose() を使ってみました。
    Dispose() は Object Class を開放するコードで this.Dispose() で Fome Class を開放します。
        protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
        {
            if ((int)(byte)e.KeyChar == (int)System.Windows.Forms.Keys.Escape)
                // プログラムを終了
                this.Dispose();
                //Application.Exit();
        }
        
  3. EscKey.cs のソースコードです。
    /*************************************************/
    /*★ ESC キーでプログラムを終了する    前田 稔 ★*/
    /*************************************************/
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class MyForm : Form
    {
        public MyForm()
        {
        }
    
        protected override void OnKeyDown(KeyEventArgs e)
        {
            switch(e.KeyCode)
            {   case Keys.Escape:
                    // プログラムを終了
                    this.Dispose();
                    //Application.Exit();
                    break;
            }
        }
    }
    
    class key
    {
        public static void Main()
        {
            MyForm mf = new MyForm();
            Application.Run(mf);
        }
    }
    

【演習】

  1. Application.Exit(); を使ってみて下さい。
  2. イベントハンドラを呼び出す方法には「デリゲートする方法」と「オーバーライドする方法」があります。
    今回はオーバーライドしましたが、デリゲートでプログラムして下さい。
  3. デリゲートで OnMyKeyDown を呼び出します。
        public MyForm()
        {   KeyDown += new KeyEventHandler(OnMyKeyDown);  }
    
  4. OnMyKeyDown() の形式です。
        protected void OnMyKeyDown(object sender, KeyEventArgs e)
        {    ・・・
        }
    

【NOTE】

new で生成した Object Class は Dispose() で開放するのが原則なのですが、C#では余り見かけません。
それはプログラマが開放しなくてもシステムが不要になった Object Class を自動的に開放してくれるからです。
この機能を Garbage Collection と言いますが、重要な Object は自分で開放することをお勧めします。
WinUser.h で定義されているキーコードの抜粋です。
#define VK_CLEAR          0x0C
#define VK_RETURN         0x0D

#define VK_SHIFT          0x10
#define VK_CONTROL        0x11
#define VK_MENU           0x12
#define VK_PAUSE          0x13
#define VK_CAPITAL        0x14

#define VK_KANA           0x15
#define VK_HANGEUL        0x15  /* old name - should be here for compatibility */
#define VK_HANGUL         0x15
#define VK_JUNJA          0x17
#define VK_FINAL          0x18
#define VK_HANJA          0x19
#define VK_KANJI          0x19

#define VK_ESCAPE         0x1B

#define VK_CONVERT        0x1C
#define VK_NONCONVERT     0x1D
#define VK_ACCEPT         0x1E
#define VK_MODECHANGE     0x1F

#define VK_SPACE          0x20
#define VK_PRIOR          0x21
#define VK_NEXT           0x22
#define VK_END            0x23
#define VK_HOME           0x24
#define VK_LEFT           0x25
#define VK_UP             0x26
#define VK_RIGHT          0x27
#define VK_DOWN           0x28
#define VK_SELECT         0x29
#define VK_PRINT          0x2A
#define VK_EXECUTE        0x2B
#define VK_SNAPSHOT       0x2C
#define VK_INSERT         0x2D
#define VK_DELETE         0x2E
#define VK_HELP           0x2F
#define VK_F1             0x70
#define VK_F2             0x71
#define VK_F3             0x72
#define VK_F4             0x73
#define VK_F5             0x74

前田稔(Maeda Minoru)の超初心者のプログラム入門