JavaScript 键盘与鼠标按键对应代码表

需积分: 9 0 下载量 58 浏览量 更新于2024-08-07 收藏 535KB PDF 举报
"鼠标和键盘对照表javascript Keycode.pdf" 在JavaScript编程中,了解键盘和鼠标事件的关键代码(Keycode)对于创建交互式用户界面至关重要。这份资源提供了一个详细的对照表,列出了不同键盘按键和鼠标操作对应的JavaScript事件属性值。以下是其中的一些关键知识点: 1. **键盘按键与Keycode**: - `event.key`:表示用户按下的键的名称,例如 `'p'` 对应于字母 'p'。 - `event.location`:指示键的位置,如标准键盘(0)、数字小键盘(1或3)等。 - `event.keyCode`:返回一个整数值,代表按下或释放的键。例如,字母 'p' 的 `keyCode` 是 80。 关键码(Keycode)是识别键盘上特定按键的一种方式,以下是一些常见Keycode的示例: - 回退键(Backspace):8 - Tab键:9 - Enter键:13 - Shift键:16 - Ctrl键:17 - Alt键:18 - 空格键:32 - 字母键:如 'a' 的 `keyCode` 是 65 2. **特殊键和功能键**: - Break键:3 - Caps Lock:20 - Page Up:33 - Page Down:34 - End键:35 - Home键:36 - 方向键:Left (37), Up (38), Right (39), Down (40) - Insert键:45 - Delete键:46 - Help键:47 3. **特殊字符和标点符号**: - 分号(Semicolon):59 - 等号(Equals):61 - 小于号(Less Than):60 - 大于号(Greater Than):62 - @字符:在Firefox中对应`event.key`是 `@"` 4. **鼠标事件**: - 鼠标的移动可以通过监听 `mousemove` 事件来捕捉,但这个对照表并未提供具体的Keycode,因为鼠标移动不产生按键事件。通常,鼠标的点击、滚动和按键会生成 `click`、`mousedown`、`mouseup`、`scroll` 等事件。 这些信息对于开发JavaScript应用,特别是涉及到用户输入、键盘快捷键或鼠标交互时非常有用。通过理解并利用这些Keycode,开发者可以精确地检测用户在网页上的每一步操作,从而实现更丰富的交互功能。

public void Move() { int stt = sun; scoreText.text = stt.ToString(); if (Input.GetKey(KeyCode.W)) { direction = 0; this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[2]; this.gameObject.transform.Translate(Vector3.up * speed * Time.deltaTime); } else if (Input.GetKey(KeyCode.S)) { this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[0]; direction = 2; this.gameObject.transform.Translate(Vector3.down * speed * Time.deltaTime); } else if (Input.GetKey(KeyCode.D)) { direction = 3; this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[1]; this.gameObject.transform.Translate(Vector3.right * speed * Time.deltaTime); } else if (Input.GetKey(KeyCode.A)) { direction = 1; this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[3]; this.gameObject.transform.Translate(Vector3.left * speed * Time.deltaTime); } // 攻击 if (Input.GetMouseButtonDown(0)) { // 处理鼠标左键点击事件 GameObject ins = Instantiate(bulletPrefab); Vector2 clickPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); ins.GetComponent<Blogs>().clickPosition = clickPosition; if (direction == 0) { ins.transform.position = new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y + 0.15f); } else if (direction == 2) { ins.transform.position = new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y - 0.15f); } else if (direction == 3) { ins.transform.position = new Vector3(this.gameObject.transform.position.x + 0.15f, this.gameObject.transform.position.y); } else if (direction == 1) { ins.transform.position = new Vector3(this.gameObject.transform.position.x - 0.15f, this.gameObject.transform.position.y); } } }增加移动时脚步声

2023-05-17 上传

public void Move() { int stt = sun; scoreText.text = stt.ToString(); if (Input.GetKey(KeyCode.W)) { direction = 0; this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[2]; this.gameObject.transform.Translate(Vector3.up * speed * Time.deltaTime); } else if (Input.GetKey(KeyCode.S)) { this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[0]; direction = 2; this.gameObject.transform.Translate(Vector3.down * speed * Time.deltaTime); } else if (Input.GetKey(KeyCode.D)) { direction = 3; this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[1]; this.gameObject.transform.Translate(Vector3.right * speed * Time.deltaTime); } else if (Input.GetKey(KeyCode.A)) { direction = 1; this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[3]; this.gameObject.transform.Translate(Vector3.left * speed * Time.deltaTime); } // 播放脚步声 if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D)) { if (!audioSource.isPlaying) { audioSource.clip = footstepSound; audioSource.loop = true; audioSource.Play(); } } else { audioSource.Stop(); } // 攻击 if (Input.GetMouseButtonDown(0)) { // 处理鼠标左键点击事件 GameObject ins = Instantiate(bulletPrefab); Vector2 clickPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); ins.GetComponent<Blogs>().clickPosition = clickPosition; if (direction == 0) { ins.transform.position = new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y + 0.15f); } else if (direction == 2) { ins.transform.position = new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y - 0.15f); } else if (direction == 3) { ins.transform.position = new Vector3(this.gameObject.transform.position.x + 0.15f, this.gameObject.transform.position.y); } else if (direction == 1) { ins.transform.position = new Vector3(this.gameObject.transform.position.x - 0.15f, this.gameObject.transform.position.y); } } }public void OnCollisionEnter2D(Collision2D collision) { if(collision.gameObject.tag == "GoldCoin") { Destroy(collision.gameObject); sun = sun + 1; if (!audioSource.isPlaying) { audioSource.clip = shique; audioSource.loop = true; audioSource.Play(); } } else { audioSource.Stop(); } //判断是否遭受攻击 if (collision.gameObject.tag == "zhi") { Destroy(collision.gameObject); hp = hp - 1; } // 检测碰撞物体的标签 if (collision.gameObject.tag == "zhi2") { // 忽略该碰撞 Physics2D.IgnoreCollision(collision.collider, GetComponent<Collider2D>()); } }优化代码

2023-05-18 上传