mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
1 line
8.5 KiB
JavaScript
1 line
8.5 KiB
JavaScript
"use strict";(self.webpackChunkwebsite=self.webpackChunkwebsite||[]).push([[20727],{3905:function(e,t,r){r.d(t,{Zo:function(){return u},kt:function(){return k}});var a=r(67294);function n(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function p(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);t&&(a=a.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,a)}return r}function o(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?p(Object(r),!0).forEach((function(t){n(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):p(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function l(e,t){if(null==e)return{};var r,a,n=function(e,t){if(null==e)return{};var r,a,n={},p=Object.keys(e);for(a=0;a<p.length;a++)r=p[a],t.indexOf(r)>=0||(n[r]=e[r]);return n}(e,t);if(Object.getOwnPropertySymbols){var p=Object.getOwnPropertySymbols(e);for(a=0;a<p.length;a++)r=p[a],t.indexOf(r)>=0||Object.prototype.propertyIsEnumerable.call(e,r)&&(n[r]=e[r])}return n}var d=a.createContext({}),i=function(e){var t=a.useContext(d),r=t;return e&&(r="function"==typeof e?e(t):o(o({},t),e)),r},u=function(e){var t=i(e.components);return a.createElement(d.Provider,{value:t},e.children)},s={inlineCode:"code",wrapper:function(e){var t=e.children;return a.createElement(a.Fragment,{},t)}},c=a.forwardRef((function(e,t){var r=e.components,n=e.mdxType,p=e.originalType,d=e.parentName,u=l(e,["components","mdxType","originalType","parentName"]),c=i(r),k=n,y=c["".concat(d,".").concat(k)]||c[k]||s[k]||p;return r?a.createElement(y,o(o({ref:t},u),{},{components:r})):a.createElement(y,o({ref:t},u))}));function k(e,t){var r=arguments,n=t&&t.mdxType;if("string"==typeof e||n){var p=r.length,o=new Array(p);o[0]=c;var l={};for(var d in t)hasOwnProperty.call(t,d)&&(l[d]=t[d]);l.originalType=e,l.mdxType="string"==typeof e?e:n,o[1]=l;for(var i=2;i<p;i++)o[i]=r[i];return a.createElement.apply(null,o)}return a.createElement.apply(null,r)}c.displayName="MDXCreateElement"},18235:function(e,t,r){r.r(t),r.d(t,{frontMatter:function(){return l},contentTitle:function(){return d},metadata:function(){return i},toc:function(){return u},default:function(){return c}});var a=r(87462),n=r(63366),p=(r(67294),r(3905)),o=["components"],l={},d=void 0,i={unversionedId:"puppeteer.keyboard",id:"version-10.1.0/puppeteer.keyboard",isDocsHomePage:!1,title:"puppeteer.keyboard",description:"Home > puppeteer > Keyboard",source:"@site/versioned_docs/version-10.1.0/puppeteer.keyboard.md",sourceDirName:".",slug:"/puppeteer.keyboard",permalink:"/puppeteer/docs/puppeteer.keyboard",editUrl:"https://github.com/facebook/puppeteer/edit/main/website/versioned_docs/version-10.1.0/puppeteer.keyboard.md",version:"10.1.0",frontMatter:{},sidebar:"version-10.1.0/docs",previous:{title:"puppeteer.accessibility.snapshot",permalink:"/puppeteer/docs/puppeteer.accessibility.snapshot"},next:{title:"puppeteer.keyboard.down",permalink:"/puppeteer/docs/puppeteer.keyboard.down"}},u=[{value:"Keyboard class",id:"keyboard-class",children:[]},{value:"Remarks",id:"remarks",children:[]},{value:"Example 1",id:"example-1",children:[]},{value:"Example 2",id:"example-2",children:[]},{value:"Methods",id:"methods",children:[]}],s={toc:u};function c(e){var t=e.components,r=(0,n.Z)(e,o);return(0,p.kt)("wrapper",(0,a.Z)({},s,r,{components:t,mdxType:"MDXLayout"}),(0,p.kt)("p",null,(0,p.kt)("a",{parentName:"p",href:"/puppeteer/docs/index"},"Home")," ",">"," ",(0,p.kt)("a",{parentName:"p",href:"/puppeteer/docs/puppeteer"},"puppeteer")," ",">"," ",(0,p.kt)("a",{parentName:"p",href:"/puppeteer/docs/puppeteer.keyboard"},"Keyboard")),(0,p.kt)("h2",{id:"keyboard-class"},"Keyboard class"),(0,p.kt)("p",null,"Keyboard provides an api for managing a virtual keyboard. The high level api is ",(0,p.kt)("a",{parentName:"p",href:"/puppeteer/docs/puppeteer.keyboard.type"},"Keyboard.type()"),", which takes raw characters and generates proper keydown, keypress/input, and keyup events on your page."),(0,p.kt)("b",null,"Signature:"),(0,p.kt)("pre",null,(0,p.kt)("code",{parentName:"pre",className:"language-typescript"},"export declare class Keyboard \n")),(0,p.kt)("h2",{id:"remarks"},"Remarks"),(0,p.kt)("p",null,"For finer control, you can use ",(0,p.kt)("a",{parentName:"p",href:"/puppeteer/docs/puppeteer.keyboard.down"},"Keyboard.down()"),", ",(0,p.kt)("a",{parentName:"p",href:"/puppeteer/docs/puppeteer.keyboard.up"},"Keyboard.up()"),", and ",(0,p.kt)("a",{parentName:"p",href:"/puppeteer/docs/puppeteer.keyboard.sendcharacter"},"Keyboard.sendCharacter()")," to manually fire events as if they were generated from a real keyboard."),(0,p.kt)("p",null,"On MacOS, keyboard shortcuts like ",(0,p.kt)("inlineCode",{parentName:"p"},"\u2318 A")," -",">"," Select All do not work. See ",(0,p.kt)("a",{parentName:"p",href:"https://github.com/puppeteer/puppeteer/issues/1313"},"#","1313"),"."),(0,p.kt)("p",null,"The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the ",(0,p.kt)("inlineCode",{parentName:"p"},"Keyboard")," class."),(0,p.kt)("h2",{id:"example-1"},"Example 1"),(0,p.kt)("p",null,"An example of holding down ",(0,p.kt)("inlineCode",{parentName:"p"},"Shift")," in order to select and delete some text:"),(0,p.kt)("pre",null,(0,p.kt)("code",{parentName:"pre",className:"language-js"},"await page.keyboard.type('Hello World!');\nawait page.keyboard.press('ArrowLeft');\n\nawait page.keyboard.down('Shift');\nfor (let i = 0; i < ' World'.length; i++)\n await page.keyboard.press('ArrowLeft');\nawait page.keyboard.up('Shift');\n\nawait page.keyboard.press('Backspace');\n// Result text will end up saying 'Hello!'\n\n")),(0,p.kt)("h2",{id:"example-2"},"Example 2"),(0,p.kt)("p",null,"An example of pressing ",(0,p.kt)("inlineCode",{parentName:"p"},"A")),(0,p.kt)("pre",null,(0,p.kt)("code",{parentName:"pre",className:"language-js"},"await page.keyboard.down('Shift');\nawait page.keyboard.press('KeyA');\nawait page.keyboard.up('Shift');\n\n")),(0,p.kt)("h2",{id:"methods"},"Methods"),(0,p.kt)("table",null,(0,p.kt)("thead",{parentName:"table"},(0,p.kt)("tr",{parentName:"thead"},(0,p.kt)("th",{parentName:"tr",align:null},"Method"),(0,p.kt)("th",{parentName:"tr",align:null},"Modifiers"),(0,p.kt)("th",{parentName:"tr",align:null},"Description"))),(0,p.kt)("tbody",{parentName:"table"},(0,p.kt)("tr",{parentName:"tbody"},(0,p.kt)("td",{parentName:"tr",align:null},(0,p.kt)("a",{parentName:"td",href:"/puppeteer/docs/puppeteer.keyboard.down"},"down(key, options)")),(0,p.kt)("td",{parentName:"tr",align:null}),(0,p.kt)("td",{parentName:"tr",align:null},"Dispatches a ",(0,p.kt)("code",null,"keydown")," event.")),(0,p.kt)("tr",{parentName:"tbody"},(0,p.kt)("td",{parentName:"tr",align:null},(0,p.kt)("a",{parentName:"td",href:"/puppeteer/docs/puppeteer.keyboard.press"},"press(key, options)")),(0,p.kt)("td",{parentName:"tr",align:null}),(0,p.kt)("td",{parentName:"tr",align:null},"Shortcut for ",(0,p.kt)("a",{parentName:"td",href:"/puppeteer/docs/puppeteer.keyboard.down"},"Keyboard.down()")," and ",(0,p.kt)("a",{parentName:"td",href:"/puppeteer/docs/puppeteer.keyboard.up"},"Keyboard.up()"),".")),(0,p.kt)("tr",{parentName:"tbody"},(0,p.kt)("td",{parentName:"tr",align:null},(0,p.kt)("a",{parentName:"td",href:"/puppeteer/docs/puppeteer.keyboard.sendcharacter"},"sendCharacter(char)")),(0,p.kt)("td",{parentName:"tr",align:null}),(0,p.kt)("td",{parentName:"tr",align:null},"Dispatches a ",(0,p.kt)("code",null,"keypress")," and ",(0,p.kt)("code",null,"input")," event. This does not send a ",(0,p.kt)("code",null,"keydown")," or ",(0,p.kt)("code",null,"keyup")," event.")),(0,p.kt)("tr",{parentName:"tbody"},(0,p.kt)("td",{parentName:"tr",align:null},(0,p.kt)("a",{parentName:"td",href:"/puppeteer/docs/puppeteer.keyboard.type"},"type(text, options)")),(0,p.kt)("td",{parentName:"tr",align:null}),(0,p.kt)("td",{parentName:"tr",align:null},"Sends a ",(0,p.kt)("code",null,"keydown"),", ",(0,p.kt)("code",null,"keypress"),"/",(0,p.kt)("code",null,"input"),", and ",(0,p.kt)("code",null,"keyup")," event for each character in the text.")),(0,p.kt)("tr",{parentName:"tbody"},(0,p.kt)("td",{parentName:"tr",align:null},(0,p.kt)("a",{parentName:"td",href:"/puppeteer/docs/puppeteer.keyboard.up"},"up(key)")),(0,p.kt)("td",{parentName:"tr",align:null}),(0,p.kt)("td",{parentName:"tr",align:null},"Dispatches a ",(0,p.kt)("code",null,"keyup")," event.")))))}c.isMDXComponent=!0}}]); |