From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../web/api/keyboardevent/charcode/index.html | 144 +++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 files/zh-cn/web/api/keyboardevent/charcode/index.html (limited to 'files/zh-cn/web/api/keyboardevent/charcode') diff --git a/files/zh-cn/web/api/keyboardevent/charcode/index.html b/files/zh-cn/web/api/keyboardevent/charcode/index.html new file mode 100644 index 0000000000..fe6523a655 --- /dev/null +++ b/files/zh-cn/web/api/keyboardevent/charcode/index.html @@ -0,0 +1,144 @@ +--- +title: KeyboardEvent.charCode +slug: Web/API/KeyboardEvent/charCode +tags: + - API + - DOM + - Deprecated + - KeyboardEvent + - events + - 键盘事件 +translation_of: Web/API/KeyboardEvent/charCode +--- +

{{ ApiRef("DOM Events") }}{{non-standard_header}}{{deprecated_header}}

+ +

{{domxref("KeyboardEvent.charCode")}} 只读属性,返回 {{ domxref("element.onkeypress", "keypress") }} 事件触发时按下的字符键的字符Unicode值。

+ +

与这些数值代码等价的常量,请参考 {{ domxref("KeyboardEvent", "KeyEvent") }}.

+ +
+

该属性已被废弃,请勿再使用该属性。

+ +

请使用 {{domxref("KeyboardEvent.key")}} 取代。

+
+ +

语法

+ +
var value = event.charCode;
+
+ + + +

示例

+ +
<html>
+<head>
+<title>charCode example</title>
+
+<script type="text/javascript">
+
+function showChar(e)
+{
+alert("Key Pressed: " + String.fromCharCode(e.charCode) + "\n"
+      + "charCode: " + e.charCode);
+}
+
+</script>
+</head>
+
+<body onkeypress="showChar(event);">
+<p>Press any 'character' type key.</p>
+</body>
+</html>
+
+ +

注意

+ +

在{{ domxref("element.onkeypress", "keypress") }} 事件中, 按键的Unicode值保存在 {{ domxref("event.keyCode", "keyCode") }} 或 {{ domxref("event.charCode", "charCode") }} 属性其中之一, 不会二者同时都有。如果按下的是字符键 (例如 'a'), charCode 被设置为字符的代码值, 并区分大小写。(即 charCode 会考虑 Shift 键是否被按下)。 否则,被按下的键的代码被存储在 keyCode 中。

+ +

如果有一个或多个修饰键被按下,有一些复杂的规则来产生 charCode 的值,细节可参考  Gecko Keypress 事件 。

+ +

charCode 用于不会在 {{ domxref("element.onkeydown", "keydown") }} 和 {{ domxref("element.onkeyup", "keyup") }} 事件中被设置。这两种情况下,keyCode 会被设置。

+ +

要获取按键代码而不考虑是 keyCode 还是charCode, 请使用 {{ domxref("event.which", "which") }} 属性。

+ +

通过输入法输入的字符,不会被设置到注册到通过 keyCode 和 charCodeActually with the Chinese IME I'm using, entering the IME results in a keypress event with keyCode = 229 and no other key events fire until the IME exits (which may happen after multiple characters are inputted). I'm not sure if other IME's work this way.

+ +

要查看特定按键的 charCode 值的列表,运行这个示例页面 Gecko DOM Reference:Examples #Example 7: Displaying Event Object Constants ,然后查看HTML 表格结果。

+ +

标准

+ + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('DOM3 Events','#widl-KeyboardEvent-charCode','KeyboardEvent.charCode')}}{{Spec2('DOM3 Events')}}Initial definition; specified as deprecated
+ +

浏览器兼容性

+ +

{{ CompatibilityTable() }}

+ +
+ + + + + + + + + + + + + + + + + + + + + +
特性ChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
基本支持26 (probably earlier){{CompatVersionUnknown}}3912.15.1 (probably earlier)
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
特性AndroidChrome for AndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
基本支持{{ CompatUnknown() }}{{ CompatUnknown() }}{{CompatVersionUnknown}}{{ CompatUnknown() }}{{ CompatUnknown() }}{{ CompatUnknown() }}5.1 (probably earlier)
+
-- cgit v1.2.3-54-g00ecf