diff options
Diffstat (limited to 'files/zh-tw/web/api/keyboardevent')
-rw-r--r-- | files/zh-tw/web/api/keyboardevent/index.html | 449 | ||||
-rw-r--r-- | files/zh-tw/web/api/keyboardevent/keyboardevent/index.html | 202 |
2 files changed, 651 insertions, 0 deletions
diff --git a/files/zh-tw/web/api/keyboardevent/index.html b/files/zh-tw/web/api/keyboardevent/index.html new file mode 100644 index 0000000000..31d75cdf1f --- /dev/null +++ b/files/zh-tw/web/api/keyboardevent/index.html @@ -0,0 +1,449 @@ +--- +title: KeyboardEvent +slug: Web/API/KeyboardEvent +tags: + - 待翻譯 +translation_of: Web/API/KeyboardEvent +--- +<p>{{APIRef("DOM Events")}}</p> + +<p><strong><code>KeyboardEvent</code></strong> objects 用來詳述使用者和網頁之間,經由鍵盤產生的互動。每個事件(<code>event</code>)都記錄著一次鍵盤動作。事件類型(<code>keydown</code> 、 <code>keypress</code> 和 <code>keyup</code>)用來表示鍵盤執行哪種動作。</p> + +<div class="note"><strong>注意:</strong> <code>KeyboardEvent</code> 僅顯示在鍵盤上發生的事。當你需要進行文字輸入的操作,請使用 HTML5 <code><a href="/en-US/docs/DOM/DOM_event_reference/input" rel="custom">input</a></code> event 代替 <code>KeyboardEvent</code> 。舉例來說,當使用者在手寫系統,例如平板電腦,輸入文字時,並不會啟動 key events 。</div> + +<h2 id="Constructor">Constructor</h2> + +<dl> + <dt>{{domxref("KeyboardEvent.KeyboardEvent", "KeyboardEvent()")}}</dt> + <dd>建立一 <code>KeyboardEvent</code> object。</dd> +</dl> + +<h2 id="Methods">Methods</h2> + +<p><em>本介面( interface)亦繼承其父, {{domxref("UIEvent")}} 和 {{domxref("Event")}} ,的 methods</em></p> + +<dl> + <dt>{{domxref("KeyboardEvent.getModifierState()")}}</dt> + <dd>回傳一 {{jsxref("Boolean")}}。用來表示當事件建立時,修飾鍵(例如 <kbd>Alt</kbd>、 <kbd>Shift</kbd>、 <kbd>Ctrl</kbd>、或是 <kbd>Meta</kbd>) 是否是按下的。</dd> + <dt>{{domxref("KeyboardEvent.initKeyEvent()")}}{{deprecated_inline}}</dt> + <dd>初始化一個 <code>KeyboardEvent</code> object。這個 method 只有 Gecko 有在使用(其他瀏覽器是使用 {{domxref("KeyboardEvent.initKeyboardEvent()")}}),並且不應該再繼續使用。現代的標準規範是使用 {{domxref("KeyboardEvent.KeyboardEvent", "KeyboardEvent()")}} constructor。</dd> + <dt>{{domxref("KeyboardEvent.initKeyboardEvent()")}}{{deprecated_inline}}</dt> + <dd>初始化一個 <code>KeyboardEvent</code> object。 Gecko 從未實作過該 method (Gecko 是使用 {{domxref("KeyboardEvent.initKeyEvent()")}}) ,並且不應該再繼續使用。現代的標準規範是使用 {{domxref("KeyboardEvent.KeyboardEvent", "KeyboardEvent()")}} constructor。</dd> +</dl> + +<h2 id="Properties">Properties</h2> + +<p><em>本介面( interface)亦繼承其父,{{domxref("UIEvent")}} 和 {{domxref("Event")}} ,的 properties 。</em></p> + +<dl> + <dt>{{domxref("KeyboardEvent.altKey")}} {{Readonlyinline}}</dt> + <dd>一個 {{jsxref("Boolean")}} 。用來表示在事件建立時, <kbd>Alt</kbd> (OS X 中是 <kbd>Option</kbd> 或 <kbd>⌥</kbd> ) 鍵是否執行中。</dd> + <dt>{{domxref("KeyboardEvent.char")}} {{Non-standard_inline}}{{Deprecated_inline}}{{Readonlyinline}}</dt> + <dd>一個 {{domxref("DOMString")}} ,返回鍵盤對應的字符。若是該鍵對應一個實際的字符,則其值為對應該字符的一個非空的 Unicode 字串;若沒對應的話,則返回一個空字串。 + <div class="note"><strong>Note:</strong> If the key is used as a macro that inserts multiple characters, this attribute's value is the entire string, not just the first character.</div> + + <div class="warning"><strong>警告:</strong> 在 DOM Level 3 Events ,該 propertie 已被移除。現在只有 IE9+ 支持它。</div> + </dd> + <dt>{{domxref("KeyboardEvent.charCode")}} {{Deprecated_inline}}{{Readonlyinline}}</dt> + <dd>Returns a {{jsxref("Number")}} representing the Unicode reference number of the key; this attribute is used only by the <code>keypress</code> event. For keys whose <code>char</code> attribute contains multiple characters, this is the Unicode value of the first character in that attribute. In Firefox 26 this returns codes for printable characters. + <div class="warning"><strong>警告:</strong> 此 attribute 已被淘汰。如果可以,建議使用 {{domxref("KeyboardEvent.key")}} 。</div> + </dd> + <dt>{{domxref("KeyboardEvent.code")}} {{Readonlyinline}}</dt> + <dd>一個 {{domxref("DOMString")}} 。返回事件對應的按鍵的代碼。</dd> + <dt>{{domxref("KeyboardEvent.ctrlKey")}} {{Readonlyinline}}</dt> + <dd>一個 {{jsxref("Boolean")}} 。用來表示在事件建立時, <kbd>Ctrl</kbd> 鍵是否執行中。</dd> + <dt>{{domxref("KeyboardEvent.isComposing")}} {{Readonlyinline}}</dt> + <dd>一個 {{jsxref("Boolean")}} 。用來表示其觸發時間是否在 <code>compositionstart</code> 和 <code>compositionend</code> 之間。</dd> + <dt>{{domxref("KeyboardEvent.key")}} {{Readonlyinline}}</dt> + <dd>一個 {{domxref("DOMString")}} ,用來事件對應的按鍵的值(key value)。</dd> + <dt>{{domxref("KeyboardEvent.keyCode")}} {{deprecated_inline}}{{Readonlyinline}}</dt> + <dd>Returns a {{jsxref("Number")}} representing a system and implementation dependent numerical code identifying the unmodified value of the pressed key. + <div class="warning"><strong>警告:</strong> 此 attribute 已被淘汰。如果可以,建議使用{{domxref("KeyboardEvent.key")}} 。</div> + </dd> + <dt>{{domxref("KeyboardEvent.locale")}} {{Readonlyinline}}</dt> + <dd>Returns a {{domxref("DOMString")}} representing a locale string indicating the locale the keyboard is configured for. This may be the empty string if the browser or device doesn't know the keyboard's locale. + <div class="note"><strong>Note:</strong> This does not describe the locale of the data being entered. A user may be using one keyboard layout while typing text in a different language.</div> + </dd> + <dt>{{domxref("KeyboardEvent.location")}}<a> {{Readonlyinline}}</a></dt> + <dd>Returns a {{jsxref("Number")}} representing the location of the key on the keyboard or other input device.</dd> + <dt>{{domxref("KeyboardEvent.metaKey")}} {{Readonlyinline}}</dt> + <dd>Returns a {{jsxref("Boolean")}} that is <code>true</code> if the <kbd>Meta</kbd> (on Mac keyboards, the <kbd>⌘ Command</kbd> key; on Windows keyboards, the Windows key (<kbd>⊞</kbd>)) key was active when the key event was generated.</dd> + <dt>{{domxref("KeyboardEvent.repeat")}} {{Readonlyinline}}</dt> + <dd>Returns a {{jsxref("Boolean")}} that is <code>true</code> if the key is being held down such that it is automatically repeating.</dd> + <dt>{{domxref("KeyboardEvent.shiftKey")}} {{Readonlyinline}}</dt> + <dd>Returns a {{jsxref("Boolean")}} that is <code>true</code> if the <kbd>Shift</kbd> key was active when the key event was generated.</dd> + <dt>{{domxref("KeyboardEvent.which")}} {{deprecated_inline}}{{Readonlyinline}}</dt> + <dd>Returns a {{jsxref("Number")}} representing a system and implementation dependent numeric code identifying the unmodified value of the pressed key; this is usually the same as <code>keyCode</code>. + <div class="warning"><strong>警告:</strong> 此 attribute 已被淘汰。如果可以,建議使用 {{domxref("KeyboardEvent.key")}} 。</div> + </dd> +</dl> + +<h2 id="注意">注意</h2> + +<p>KeyboardEvent 有 <code>keydown</code> 、 <code>keypress</code> 、 <code>keyup</code> 三種事件。對大多數的按鍵而言, Gecko 觸發事件的順序如下:</p> + +<ol> + <li>當按鍵按下時,會送出 <code>keydown</code> event 。</li> + <li>當按鍵不是特殊鍵( modifier key),例如<kbd>Ctrl</kbd> 、<kbd>Alt</kbd>……等等,會送出 <code>keypress</code> event 。</li> + <li>當按鍵放開時,會送出 <code>keyup</code> event 。</li> +</ol> + +<h3 id="特殊狀況">特殊狀況</h3> + +<p>某些按鍵,例如 <kbd>Caps Lock</kbd> 、 <kbd>Num Lock</kbd> 和 <kbd>Scroll Lock</kbd> 能切換鍵盤上的 LED 燈。在 Windows 和 Linux 系統上,這些按鍵只會觸發 <code>keydown</code> 和 <code>keyup</code> 事件。但是 Linux 上的 Firefox 12 或更早的版本亦會觸發 <code>keypress</code> 事件。</p> + +<p>而在 Mac 電腦則不同, <kbd>Caps Lock</kbd> 只會觸發 <code>keydown</code> 事件;而 <kbd>Num Lock</kbd> 則是只有舊版的 Mac 電腦(2007 或之前的版本)才有,現在的 Mac 即便使用外部鍵盤也不支援 <kbd>Num Lock</kbd> 。雖說舊版的 Mac 電腦支援 <kbd>Num Lock</kbd> 鍵,但 <kbd>Num Lock</kbd> 並不會執行任何 KeyboardEvent;而 Gecko 瀏覽器在特殊情況(外接一個有 <kbd>F14</kbd> 的鍵盤)下能支援 <kbd>Scroll Lock</kbd> ,但是它會產生 <code>keypress</code> 事件。這個異常狀態是個 bug ,詳情可參考 {{bug(602812)}}。</p> + +<h3 id="自動迴圈(Auto-Repeat_)的執行">自動迴圈(Auto-Repeat )的執行</h3> + +<p>當按鍵按下去不放時,它會開始一個自動迴圈。並導致觸發一系列的相似事件,如下所示:</p> + +<ol> + <li><code>keydown</code></li> + <li><code>keypress</code></li> + <li><code>keydown</code></li> + <li><code>keypress</code></li> + <li>(不斷重複,直到使用者放開按鍵)</li> + <li><code>keyup</code></li> +</ol> + +<p>在 DOM Level 3 說明書有提及這問題是會發生的。其中所存在的問題如下說明:</p> + +<h4 id="部分_GTK_環境,例如_Ubuntu_9.4_,的自動迴圈">部分 GTK 環境,例如 Ubuntu 9.4 ,的自動迴圈</h4> + +<p>部分的 GTK-based 環境之中,自動迴圈在發生的過程中會自動觸發電腦本機的 key-up 事件。然而,對 Gecko 而言,並沒有方法可以分辨使用者重複點擊按鍵與自動迴圈(按鍵按住不放)的差異。在這類的環境下,按鍵按住不放會重複執行下列事件:</p> + +<ol> + <li><code>keydown</code></li> + <li><code>keypress</code></li> + <li><code>keyup</code></li> + <li><code>keydown</code></li> + <li><code>keypress</code></li> + <li><code>keyup</code></li> + <li>(不斷重複,直到使用者放開按鍵)</li> + <li><code>keyup</code></li> +</ol> + +<p>不幸地,在這些環境之下,web content 亦沒有方法告訴使用者重複點擊按鍵與自動迴圈的差異。</p> + +<h4 id="Gecko_5.0_以前的自動迴圈">Gecko 5.0 以前的自動迴圈</h4> + +<p>Gecko 5.0 {{geckoRelease('5.0')}} 以前,在不同平台上,鍵盤的處理與現在相比較不統一。</p> + +<dl> + <dt>Windows</dt> + <dd>自動迴圈的結果與 Gecko 4.0 或更新的版本類似</dd> + <dt>Mac</dt> + <dd>在第一個 keydown 執行後,僅執行 keypress 事件,一直到案件放開(即送出 keyup 事件指令),過程中不會送出任何 keydown 事件。</dd> + <dt>Linux</dt> + <dd>鍵盤事件的執行根據平台不同而有所不同。它有可能表現得像是 Windows 也有可能像 Mac ,這取決於本地的事件模型(native event model)是如何執行的。</dd> +</dl> + +<h2 id="範例">範例</h2> + +<pre class="brush: js"><!DOCTYPE html> +<html> +<head> +<script> +var metaChar = false; +var exampleKey = 16; + +function keyEvent(event) { + var key = event.keyCode || event.which; + var keychar = String.fromCharCode(key); + if (key == exampleKey) { + metaChar = true; + } + if (key != exampleKey) { + if (metaChar) { + alert("Combination of metaKey + " + keychar); + metaChar = false; + } else { + alert("Key pressed " + key); + } + } +} + +function metaKeyUp (event) { + var key = event.keyCode || event.which; + + if (key == exampleKey) { + metaChar = false; + } +} +</script> +</head> + +<body onkeydown="keyEvent(event)" onkeyup="metaKeyUp(event)"> +</body> +</html> +</pre> + +<h2 id="規格">規格</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('DOM3 Events', '#interface-KeyboardEvent', 'KeyboardEvent')}}</td> + <td>{{Spec2('DOM3 Events')}}</td> + <td>原始定義</td> + </tr> + </tbody> +</table> + +<p>The <code>KeyboardEvent</code> interface specification went through numerous draft versions, first under DOM Events Level 2 where it was dropped as no consensus arose, then under DOM Events Level 3. This led to the implementation of non-standard initialization methods, the early DOM Events Level 2 version, {{domxref("KeyboardEvent.initKeyEvent()")}} by Gecko browsers and the early DOM Events Level 3 version, {{domxref("KeyboardEvent.initKeyboardEvent()")}} by others. Both have been superseded by the modern usage of a constructor: {{domxref("KeyboardEvent.KeyboardEvent", "KeyboardEvent()")}}.</p> + +<h2 id="瀏覽器支援度">瀏覽器支援度</h2> + +<p>More compatibility data is available on other pages:</p> + +<ul> + <li><code>.code</code> :請參考《瀏覽器支援度》的 {{domxref("KeyboardEvent.code")}} 部分。</li> + <li><code>.key</code>:請參考《瀏覽器支援度》的 {{domxref("KeyboardEvent.key")}} 部分。</li> + <li><code>.getModifierState()</code> :請參考《瀏覽器支援度》的 {{domxref("KeyboardEvent.getModifierState")}} 部分。</li> +</ul> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari (WebKit)</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + <tr> + <td>constructor</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop("31.0")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + <tr> + <td><code>.char</code></td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + <tr> + <td><code>.charCode</code></td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + <tr> + <td><code>.code</code></td> + <td colspan="5" rowspan="1">See Browser compatibility of {{domxref("KeyboardEvent.code")}}.</td> + </tr> + <tr> + <td><code>.isComposing</code></td> + <td>{{CompatNo}}</td> + <td>{{CompatGeckoDesktop("31.0")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + <tr> + <td><code>.key</code></td> + <td colspan="5" rowspan="1">See Browser compatibility of {{domxref("KeyboardEvent.key")}}.</td> + </tr> + <tr> + <td><code>.keyCode</code></td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + <tr> + <td><code>.locale</code></td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + <tr> + <td><code>.location</code></td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop("15.0")}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + <tr> + <td><code>.repeat</code></td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop("28.0")}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + <tr> + <td><code>.which</code></td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + <tr> + <td><code>.getModifierState()</code></td> + <td colspan="5" rowspan="1">See Browser compatibility of {{domxref("KeyboardEvent.getModifierState")}}</td> + </tr> + <tr> + <td><code>.initKeyboardEvent()</code></td> + <td>{{CompatVersionUnknown}}<sup>[1]</sup></td> + <td>{{CompatNo}}<sup>[2]</sup></td> + <td>{{CompatIE("9.0")}}<sup>[3]</sup></td> + <td>{{CompatUnknown}}</td> + <td>{{CompatVersionUnknown}}<sup>[1]</sup></td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Phone</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + <tr> + <td>constructor</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatGeckoMobile("31.0")}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + <tr> + <td><code>.char</code></td> + <td>{{CompatUnknown}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + <tr> + <td><code>.charCode</code></td> + <td>{{CompatUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + <tr> + <td><code>.code</code></td> + <td colspan="5" rowspan="1">See Browser compatibility of {{domxref("KeyboardEvent.code")}}.</td> + </tr> + <tr> + <td><code>.isComposing</code></td> + <td>{{CompatNo}}</td> + <td>{{CompatGeckoMobile("31.0")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + <tr> + <td><code>.key</code></td> + <td colspan="6">See Browser compatibility table of {{domxref("KeyboardEvent.key")}}.</td> + </tr> + <tr> + <td><code>.keyCode</code></td> + <td>{{CompatUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + <tr> + <td><code>.locale</code></td> + <td>{{CompatUnknown}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + <tr> + <td><code>.location</code></td> + <td>{{CompatUnknown}}</td> + <td>{{CompatGeckoMobile("15.0")}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + <tr> + <td><code>.repeat</code></td> + <td>{{CompatUnknown}}</td> + <td>{{CompatGeckoMobile("28.0")}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + <tr> + <td><code>.which</code></td> + <td>{{CompatUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + <tr> + <td><code>.getModifierState()</code></td> + <td colspan="5" rowspan="1">See Browser compatibility of {{domxref("KeyboardEvent.getModifierState")}}</td> + </tr> + <tr> + <td><code>.initKeyboardEvent()</code></td> + <td>{{CompatUnknown}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<p>[1] The arguments of <code>initKeyboardEvent()</code> of WebKit and Blink's are different from the definition in DOM Level 3 Events. The method is: <code>initKeyboardEvent(in DOMString typeArg, in boolean canBubbleArg, in boolean cancelableArg, in views::AbstractView viewArg, in DOMString keyIndentifierArg, in number locationArg, in boolean ctrlKeyArg, in boolean altKeyArg, in boolean shiftKeyArg, in boolean metaKeyArg, in boolean altGraphKeyArg)</code></p> + +<p>[2] Gecko won't support <code>initKeyboardEvent()</code> because supporting it completely breaks feature detection of web applications. See {{Bug(999645)}}.</p> + +<p>[3] The argument of <code>initKeyboardEvent()</code> of IE is different from the definition in DOM Level 3 Events. The method is: <code>initKeyboardEvent(in DOMString typeArg, in boolean canBubbleArg, in boolean cancelableArg, in views::AbstractView viewArg, in DOMString keyArg, in number locationArg, in DOMString modifierListArg, in boolean repeatArt, in DOMString locationArg)</code>. See <a href="http://msdn.microsoft.com/en-us/library/ie/ff975297%28v=vs.85%29.aspx">document of <code>initKeyboardEvent()</code> in MSDN</a>.</p> + +<p><a href="http://www.howtocreate.co.uk/tutorials/javascript/domevents#domevld1">[4]</a> Note that manually firing an event does <em>not</em> generate the default action associated with that event. For example, manually firing a key event does not cause that letter to appear in a focused text input. In the case of UI events, this is important for security reasons, as it prevents scripts from simulating user actions that interact with the browser itself.</p> diff --git a/files/zh-tw/web/api/keyboardevent/keyboardevent/index.html b/files/zh-tw/web/api/keyboardevent/keyboardevent/index.html new file mode 100644 index 0000000000..3af92514bb --- /dev/null +++ b/files/zh-tw/web/api/keyboardevent/keyboardevent/index.html @@ -0,0 +1,202 @@ +--- +title: KeyboardEvent() +slug: Web/API/KeyboardEvent/KeyboardEvent +translation_of: Web/API/KeyboardEvent/KeyboardEvent +--- +<p>{{APIRef("DOM Events")}}</p> + +<p><strong><code>KeyboardEvent()</code></strong> constructor 能用來建立一個新的 {{domxref("KeyboardEvent")}}。</p> + +<h2 id="語法">語法</h2> + +<pre class="syntaxbox"> <em>event</em> = new KeyboardEvent(<em>typeArg</em>, <em>KeyboardEventInit</em>);</pre> + +<h3 id="參數">參數</h3> + +<dl> + <dt><em>typeArg</em></dt> + <dd>一 {{domxref("DOMString")}} 用來表示事件名稱。</dd> + <dt><em>KeyboardEventInit</em>{{optional_inline}}</dt> + <dd>一個 <code>KeyboardEventInit</code> dictionary,能接受以下參數: + <table class="standard-table"> + <thead> + <tr> + <th>參數</th> + <th>可選</th> + <th>默認值</th> + <th>類型</th> + <th>說明</th> + </tr> + </thead> + <tbody> + <tr> + <td><code>"key"</code></td> + <td>●</td> + <td><code>""</code></td> + <td>{{domxref("DOMString")}}</td> + <td>用來設定 {{domxref("KeyboardEvent.key")}} 的值</td> + </tr> + <tr> + <td><code>"code"</code></td> + <td>●</td> + <td><code>""</code></td> + <td>{{domxref("DOMString")}}</td> + <td>用來設定 {{domxref("KeyboardEvent.code")}} 的值</td> + </tr> + <tr> + <td><code>"location"</code></td> + <td>●</td> + <td>0</td> + <td><code>unsigned long</code></td> + <td>用來設定 {{domxref("KeyboardEvent.location")}} 的值</td> + </tr> + <tr> + <td><code>"ctrlKey"</code></td> + <td>●</td> + <td><code>false</code></td> + <td>{{jsxref("Boolean")}}</td> + <td>用來設定 {{domxref("KeyboardEvent.ctrlKey")}} 的值</td> + </tr> + <tr> + <td><code>"shiftKey"</code></td> + <td>●</td> + <td><code>false</code></td> + <td>{{jsxref("Boolean")}}</td> + <td>用來設定 {{domxref("KeyboardEvent.shiftKey")}} 的值</td> + </tr> + <tr> + <td>"altKey"</td> + <td>●</td> + <td><code>false</code></td> + <td>{{jsxref("Boolean")}}</td> + <td>用來設定 {{domxref("KeyboardEvent.altKey")}} 的值</td> + </tr> + <tr> + <td>"metaKey"</td> + <td>●</td> + <td><code>false</code></td> + <td>{{jsxref("Boolean")}}</td> + <td>用來設定 {{domxref("KeyboardEvent.metaKey")}} 的值</td> + </tr> + <tr> + <td>"repeat"</td> + <td>●</td> + <td><code>false</code></td> + <td>{{jsxref("Boolean")}}</td> + <td>用來設定 {{domxref("KeyboardEvent.repeat")}} 的值</td> + </tr> + <tr> + <td>"isComposing"</td> + <td>●</td> + <td><code>false</code></td> + <td>{{jsxref("Boolean")}}</td> + <td>用來設定 {{domxref("KeyboardEvent.isComposing")}} 的值</td> + </tr> + <tr> + <td>"charCode"</td> + <td>●</td> + <td>0</td> + <td><code>unsigned long</code></td> + <td>用來設定 {{domxref("KeyboardEvent.charCode")}} 的值</td> + </tr> + <tr> + <td>"keyCode"</td> + <td>●</td> + <td>0</td> + <td><code>unsigned long</code></td> + <td>用來設定 {{domxref("KeyboardEvent.keyCode")}} 的值</td> + </tr> + <tr> + <td>"which"</td> + <td>●</td> + <td>0</td> + <td><code>unsigned long</code></td> + <td>用來設定 {{domxref("KeyboardEvent.which")}} 的值</td> + </tr> + </tbody> + </table> + + <div class="note"> + <p><em> <code>KeyboardEventInit</code> dictionary 亦接受 {{domxref("UIEvent.UIEvent", "UIEventInit")}} 和{{domxref("Event.Event", "EventInit")}} 所接受的參數。</em></p> + </div> + </dd> +</dl> + +<h2 id="Specifications" name="Specifications">規格</h2> + +<table class="standard-table" style="height: 49px; width: 1000px;"> + <thead> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('DOM3 Events','#interface-KeyboardEvent','KeyboardEvent()')}}</td> + <td>{{Spec2('DOM3 Events')}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="瀏覽器支援度">瀏覽器支援度</h2> + +<p>{{ CompatibilityTable() }}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatGeckoDesktop(13) }}</td> + <td>{{ CompatNo() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatUnknown() }}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Android Webview</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + <th>Chrome for Android</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{ CompatUnknown }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatGeckoMobile(31) }}</td> + <td>{{ CompatNo() }}</td> + <td>{{ CompatVersionUnknown() }}</td> + <td>{{ CompatUnknown() }}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="延伸閱讀">延伸閱讀</h2> + +<ul> + <li>{{domxref("KeyboardEvent")}} ,此 constructer 所建立的 object 的 interface</li> +</ul> |