▼XNA Game Studioメモ▼
キーイベントの処理


押したキーの名前を画面に表示するプログラムを作成する。表示されるキーは上下左右、エンター、スペースキー。



リソースの追加
  1. はじめてのXNAアプリの作成」で作成したフォントプロセッサ「DefaultFont.spritefont」をソリューションエクスプローラのContentに追加する。

Keys列挙型
A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z 文字キー
F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,
F11,F12,F13,F14,F15,F16,F17,F18,F19.F20,
F21,F22,F23,F24
ファンクションキー
NumPad0,NumPad1,NumPad2,NumPad3,NumPad4,
NumPad5,NumPad6,NumPad7,NumPad8,NumPad9,
Decimal,Divide,
数字パッドキー
Up,Down,Left,Right 方向キー
LeftAlt,LeftControl,LeftShift,LeftWindows
RightAlt,RightControl,RightShift,RightWindows
修飾キー
Add,Apps,Attn,Back,CapsLock,Crsel,
Delete,End,Enter,EraseEof,Escape,Execute,Exsel
Help,Home,Insert,Multiply,None,NumLock
Pa1,PageDown,PageUp,Play,Print,PrintScreen,
ProcessKey,Scroll,Select,SelectMedia,
Separator,Sleep,Space,Subtract,Tab,Zoom
機能キー
LaunchApplication1,LaunchApplication2,LaunchMail 起動キー
VolumeDown,VolumeMute,VolumeUp ボリュームキー
MediaNextTrack,MediaPlayPause,MediaPreviousTrack,MediaStop メディアキー
BrowserBack,BrowserFavorites,BrowserForward,,
BrowserHome,BrowserRefresh,BrowserSearch,BrowserStop
ブラウザキー
Oem8,OemBackslash,OemClear,OemCloseBrackets,OemComma
OemMinus,OemOpenBrackets,OemPeriod,OemPipe,OemPlus,
OemQuestion,OemQuotes,OemSemicolon,OemTilde
Oemキー
D0,D1,D2,D3,D4,D5,D6,D7,D8,D9 キーボードによって異なるキー


ソースコードの編集
Game.cs
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace KeyEx {
    public class Game : Microsoft.Xna.Framework.Game {
        private GraphicsDeviceManager graphics;   //グラフィックス
        private SpriteBatch           spriteBatch;//スプライトバッチ
        private SpriteFont            font;       //フォント
        private String                info;       //情報

        //コンストラクタ
        public Game() {
            graphics=new GraphicsDeviceManager(this);
            Content.RootDirectory="Content";
        }

        //初期化
        protected override void Initialize() {
            base.Initialize();
        }

        //グラフィクスコンテントの読み込み
        protected override void LoadContent() {
            spriteBatch=new SpriteBatch(GraphicsDevice);
            font=Content.Load<SpriteFont>("DefaultFont");
        }

        //グラフィクスコンテントの解放
        protected override void UnloadContent() {
        }

        //更新
        protected override void Update(GameTime gameTime) {
            //アプリ終了
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back==
                ButtonState.Pressed) {
                Exit();
            }

            //キー状態の取得
            info="Key>";
            KeyboardState keyState=Keyboard.GetState();
            if (keyState.IsKeyDown(Keys.Left))  info+="Left ";
            if (keyState.IsKeyDown(Keys.Right)) info+="Right ";
            if (keyState.IsKeyDown(Keys.Up))    info+="Up ";
            if (keyState.IsKeyDown(Keys.Down))  info+="Down ";
            if (keyState.IsKeyDown(Keys.Enter)) info+="Enter";
            if (keyState.IsKeyDown(Keys.Space)) info+="Space";

            //ベースの更新
            base.Update(gameTime);
        }

        //描画
        protected override void Draw(GameTime gameTime) {
            //背景の描画
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

            //文字列の描画
            spriteBatch.Begin();
            DrawString(info,0,0);
            spriteBatch.End();

            //ベースの描画
            base.Draw(gameTime);
        }

        //文字列の描画
        private void DrawString(String str,int x,int y) {
            spriteBatch.DrawString(font,str,new Vector2(x,y),Color.White);
        }
    }
}



−戻る−