PageSourceSearch

https://www.monogameextended.net/assets/js/ef97bd42.f46cee52.js

js monogameextended.net collected 2026-10-03 23:19:48 UTC 6,528 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunkmonogame_extended_website=self.webpackChunkmonogame_extended_website||[]).push([[1706],{6103:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>s,contentTitle:()=>a,default:()=>l,frontMatter:()=>r,metadata:()=>i,toc:()=>c});var d=n(4848),o=n(8453);const r={id:"keyboardextended",title:"KeyboardExtended",sidebar_label:"KeyboardExtended"},a=void 0,i={id:"features/input/keyboardextended/keyboardextended",title:"KeyboardExtended",description:"This page is up to date for MonoGame.Extended @mgeversion@. If you find outdated information, please open an issue.",source:"@site/docs/features/input/keyboardextended/keyboardextended.md",sourceDirName:"features/input/keyboardextended",slug:"/features/input/keyboardextended/",permalink:"/docs/features/input/keyboardextended/",draft:!1,unlisted:!1,editUrl:"https://github.com/monogame-extended/monogame-extended.github.io/tree/develop/docs/features/input/keyboardextended/keyboardextended.md",tags:[],version:"current",frontMatter:{id:"keyboardextended",title:"KeyboardExtended",sidebar_label:"KeyboardExtended"},sidebar:"docs",previous:{title:"Bitmap Font",permalink:"/docs/features/fonts/bitmapfont/"},next:{title:"MouseExtended",permalink:"/docs/features/input/mouseextended/"}},s={},c=[{value:"Using <code>KeyboardExtended</code>",id:"using-keyboardextended",level:2},{value:"Using <code>KeyboardStateExtended</code>",id:"using-keyboardstateextended",level:2}];function h(e){const t={a:"a",admonition:"admonition",code:"code",h2:"h2",p:"p",pre:"pre",strong:"strong",...(0,o.R)(),...e.components};return(0,d.jsxs)(d.Fragment,{children:[(0,d.jsx)(t.admonition,{title:"Up to date",type:"tip",children:(0,d.jsxs)(t.p,{children:["This page is ",(0,d.jsx)(t.strong,{children:"up to date"})," for MonoGame.Extended ",(0,d.jsx)(t.code,{children:"6.0.0"}),". If you find outdated information, ",(0,d.jsx)(t.a,{href:"https://github.com/monogame-extended/monogame-extended.github.io/issues",children:"please open an issue"}),"."]})}),"\n",(0,d.jsxs)(t.p,{children:[(0,d.jsx)(t.code,{children:"KeyboardExtended"})," is an extension of the ",(0,d.jsx)(t.code,{children:"Microsoft.Xna.Framework.Input.Keyboard"})," class. Unlike the default MonoGame implementation, ",(0,d.jsx)(t.code,{children:"KeyboardExtended"})," will internally track both the keyboard state of the previous frame and the keyboard state of the current frame. This allows for easy comparisons between the previous and current state to check for single frame key presses."]}),"\n",(0,d.jsxs)(t.p,{children:[(0,d.jsx)(t.code,{children:"KeyboardExtended"})," also contains the ",(0,d.jsx)(t.code,{children:"KeyboardExtended.GetState()"})," method which returns back a ",(0,d.jsx)(t.code,{children:"KeyboardStateExtended"})," struct. This struct provides the standard utility methods of checking if a key is down or up, but also extends this functionality with methods for checking for single frame key presses."]}),"\n",(0,d.jsxs)(t.h2,{id:"using-keyboardextended",children:["Using ",(0,d.jsx)(t.code,{children:"KeyboardExtended"})]}),"\n",(0,d.jsxs)(t.p,{children:[(0,d.jsx)(t.code,{children:"KeyboardExtended"})," is a ",(0,d.jsx)(t.code,{children:"static"})," class, so you do not need to instantiate it in the different areas you use it or pass the dependency around to different objects. However it does need to be updated once per update frame. The best place to do this is in your ",(0,d.jsx)(t.code,{children:"Game"})," class."]}),"\n",(0,d.jsx)(t.pre,{children:(0,d.jsx)(t.code,{className:"language-cs",children:"protected override void Update(GameTime gameTime)\r\n{\r\n    // highlight-next-line\r\n    KeyboardExtended.Update();\r\n}\n"})}),"\n",(0,d.jsx)(t.admonition,{type:"caution",children:(0,d.jsxs)(t.p,{children:[(0,d.jsx)(t.code,{children:"KeyboardExtended.Update"})," should only be called once per update cycle in your game. This is because when calling it, it will update its internal source for the previous state by caching the current state into the previous state before refreshing the current state. Calling this method more than once during an update cycle can cause the previous state cache to be over
1written with invalid data."]})}),"\n",(0,d.jsxs)(t.p,{children:["Then, just like with base MonoGame, when you need to get the state of keyboard input from ",(0,d.jsx)(t.code,{children:"KeyboardExtended"})," you use the ",(0,d.jsx)(t.code,{children:"KeyboardExtended.GetState()"})," method. This returns back a new ",(0,d.jsx)(t.code,{children:"KeyboardStateExtended"})," struct"]}),"\n",(0,d.jsx)(t.pre,{children:(0,d.jsx)(t.code,{className:"language-cs",children:"protected override void Update(GameTime gameTime)\r\n{\r\n    KeyboardExtended.Update();\r\n    // highlight-next-line\r\n    KeyboardStateExtended keyboardState = KeyboardExtended.GetState();\r\n}\n"})}),"\n",(0,d.jsxs)(t.h2,{id:"using-keyboardstateextended",children:["Using ",(0,d.jsx)(t.code,{children:"KeyboardStateExtended"})]}),"\n",(0,d.jsxs)(t.p,{children:["At any time, you can call ",(0,d.jsx)(t.code,{children:"KeyboardExtended.GetState()"})," to get the ",(0,d.jsx)(t.code,{children:"KeyboardStateExtended"})," struct. This struct is an extended version of the base MonoGame ",(0,d.jsx)(t.code,{children:"KeyboardState"})," that provides utility for checking keyboard states between the previous and current frame. By providing this utility, you can not only check if a key is down, but also check for single-frame presses of a key."]}),"\n",(0,d.jsx)(t.pre,{children:(0,d.jsx)(t.code,{className:"language-cs",children:"protected override void Update(GameTime gameTime)\r\n{\r\n    KeyboardExtended.Update();\r\n    KeyboardStateExtended keyboardState = KeyboardExtended.GetState();\r\n\r\n\r\n    // highlight-next-line\r\n    if(keyboardState.WasKeyPressed(Keys.Space))\r\n    // highlight-next-line\r\n    {\r\n        // highlight-next-line\r\n        // Do something when detecting a single frame press of the space key.\r\n    // highlight-next-line\r\n    }\r\n}\n"})})]})}function l(e={}){const{wrapper:t}={...(0,o.R)(),...e.components};return t?(0,d.jsx)(t,{...e,children:(0,d.jsx)(h,{...e})}):h(e)}},8453:(e,t,n)=>{n.d(t,{R:()=>a,x:()=>i});var d=n(6540);const o={},r=d.createContext(o);function a(e){const t=d.useContext(r);return d.useMemo((function(){return"function"==typeof e?e(t):{...t,...e}}),[t,e])}function i(e){let t;return t=e.disableParentContext?"function"==typeof e.components?e.components(o):e.components||o:a(e.components),d.createElement(r.Provider,{value:t},e.children)}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.