App.jsx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import React, { useState, useRef } from "react"; import Keyboard from "react-simple-keyboard"; import "react-simple-keyboard/build/css/index.css"; const App = () => { const [layoutName, setLayoutName] = useState("default"); const [input, setInput] = useState(""); const keyboardRef = useRef(null); const onChange = (input) => { setInput(input); console.log("Input changed", input); }; const onKeyPress = (button) => { console.log("Button pressed", button); /** * If you want to handle the shift and caps lock buttons */ if (button === "{shift}" || button === "{lock}") handleShift(); }; const handleShift = () => { setLayoutName((prevLayout) => (prevLayout === "default" ? "shift" : "default")); }; const onChangeInput = (event) => { const input = event.target.value; setInput(input); if (keyboardRef.current) { keyboardRef.current.setInput(input); } }; return ( <div> <input value={input} placeholder={"Tap on the virtual keyboard to start"} onChange={onChangeInput} /> <Keyboard keyboardRef={(r) => (keyboardRef.current = r)} layoutName={layoutName} onChange={onChange} onKeyPress={onKeyPress} /> </div> ); }; export default App |