简体   繁体   English

如何仅使用键盘 React 移动数字

[英]How can I move figures only with keyboard React

export const Chessboard: React.FC = () => {
  const board = [];

  for (let j = horisontalAxis.length - 1; j >= 0; j -= 1) {
    for (let i = 0; i < verticalAxis.length; i += 1) {
      const num = j + i + 2;
      let image = '';

      figures.forEach(el => {
        if (el.x === i && el.y === j) {
          image = el.image;
        }
      });

      board.push(<Cell key={`${i}, ${j}`} image={image} num={num} />);
    }
  }

  return (
    <div className="chessboard">{board}</div>
  );
};

I am trying to make chess game that works only with keyboard.我正在尝试制作仅适用于键盘的国际象棋游戏。 How can I select figure with keyboard without mouse and make move also?如何在没有鼠标的情况下使用键盘选择图形并进行移动?

Implement a cursor.实现一个游标。 Make a state that holds the current coordinates of the cursor (x,y).创建一个保存光标当前坐标 (x,y) 的状态。 Also draw the cursor on your board (like a border around the cell).还要在板上绘制光标(如单元格周围的边框)。 Then listen for key presses of the arrow keys and use this to update the cursor position.然后监听箭头键的按键并使用它来更新光标位置。

Then you need some more keys (space, enter), and those make you select a piece at the cursor position, or put down a selected piece.然后你需要更多的键(空格,回车),这些让你在光标位置选择一块,或者放下一个选定的块。 This adds some more state (held piece, which could be null), and you need to think about how to visualize this.这会增加一些状态(持有的部分,可能为空),您需要考虑如何将其可视化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何避免因 React Native 上的键盘而调整视图大小 - How can I avoid resizing of a view because of keyboard on React Native 如何在React中将子组件移到新的父组件? - How can I move my child component to new parent in React? 如何让我的画廊在 React 中移动图像? - how can I make my gallery move the images in React? 当键盘出现在 React Native 中时如何移动视图? - How to move a view when the keyboard shows up in React Native? 如何在 React 中使用 onScroll 事件使用 React-Router 移动到另一个页面 - How can I use an onScroll event in React to move to another page using React-Router ChartJs React - 如何阻止 ChartJs 在工具提示上将我的数字四舍五入到小数点后 3 位? - ChartJs React - How do I stop ChartJs from rounding my figures to 3 decimal points on the tooltip? 由于使用 React Native 的虚拟键盘,复选框被选中但没有注册值。 我怎样才能解决这个问题? - Checkboxes getting checked but no value registered because of virtual keyboard using React Native. How can I fix this? 如何在类型编号的 html 输入上禁用键盘输入? 反应或 material-ui 怎么样? - How can I disable keyboard input on an html input of type number? What about in react or material-ui? 在反应中使用键盘箭头在 div 中移动 - Move through divs using the keyboard arrows on react React - 如何编写像netflix这样的键盘应用程序? - React - How to write keyboard only apps like netflix?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM