This adds a simple on-screen keyboard, allowing users who can't use a keyboard (mobile users for example) to interact with projects that depend on basic key presses. To use the keyboard in this project, click on the keyboard icon, and then on any of the keys. Specifically designed to support @boahthenoah’s duck project. Useful if you have a project that uses basic single key key presses. Not suitable for complex shortcuts or games. To add the on-screen keyboard to your own project, follow these steps: 1. Copy the 'osk' sprite to your project, using the Scratch backpack. 2. Instead of using 'When () key pressed' to handle key presses, use 'When I receive ()' blocks. For more information about the message names, see below. 3. Your project will now respond to a key being pressed, whether it's on a physical keyboard or the on-screen keyboard. 4. To stop seeing the "broadcasting" messages, either set the 'muteAnnouncements' variable to true, or detach the "think" block in the 'handleButtonClicked' block. Broadcast messages: Each key broadcasts its own message. These message names always starts with 'key-', followed by the key character. For example, when you press 'x' (either on your own keyboard or on the on-screen keyboard), the message 'key-x' will be broadcasted. Some keys messages have longer names, such as 'key-downArrow'. If you're not sure of what message name to use for a key, press it in this project and see what message Josh is receiving. Or better yet, look in the code for the Josh sprite, and copy the applicable "when I receive" block there. You can also copy the entire Josh sprite so that you have all message handler blocks ready to go, and then add your own costumes and additional code there. Shift key The on-screen keyboard has a 'caps lock' button, which lets you toggle between regular keys and shift keys. This allows you to use keys like '$' and '>'. The on-screen keyboard broadcasts messages for capital letters with the word 'cap' in front of the letter, for example "key-capQ". There's also a 'shift' button, which automatically changes the keyboard back to regular keys once one key is pressed. Notes and limitations - Not the easiest to use on small screens. - From what I understand, it's not possible in Scratch to detect capital letters, esc, tab, ctrl, alt, start, or backspace keypresses from your regular keyboard. - Since Scratch won't let you detect which object in a sprite was clicked like other programming languages would, I'm using mouseX,mouseY coordinates to calculate which button was clicked. This is a very brittle approach, and not ideal. For example, the keyboard won't function properly if you resize the sprite. You can however reposition the sprite in your project, since the logic accounts for the sprite's x and y position in these calculations. Remixing: It's possible to remix this project and add your own keys, but it's not easy to do. You'll have to: 1. Draw the extra keys in the costume. Note that for this keyboard to work, all keys must have the same size and spacing (although keys can take up the width of two or more regular keys). The easiest approach is to ungroup the keyboard and copy, paste, and edit an existing key. Alt-dragging seemed buggy to me (caused them to be duplicated many times), but using Ctrl+C and Ctrl-V worked well for me. 2. Add your new character or message name to either the 'allKeys' or 'allKeysShift' list, depending on which costume you're editing. The easiest way to update these lists is to make them visible in the project, right click on them, and choose "import". I have the current items for allKeys and allKeysShift in separate txt documents, but since they're very long I won't add them here. If you need these lists, leave a comment. If you want a key to be wider than a single key, copy that item multiple times in the list. For example, both allKeys and allKeysShift contain 6 'space' items, because the Space key is the same width as six regular keys. If you want there to be a gap in a row of keys, add an empty item to the list. 3. Depending on which row the new keys were added for, the values in the 'keyRowIndices' list must be adjusted as well. Each value in this list specifies the index each row starts at in allKeys and allKeysShift. 4. If you drag a row to the left or right to create more space, you'll have to adjust the 'keyRowXOffsets' list. This specifies how many pixels there are between each row and the left bound of the project. Note that the keyboard toggle icon is a row in itself, so there is lots of space there for new keys. (this is also why there are 6 rows in the code, even though it looks like there's only 5 in the costumes. The keyboard toggle icon is row 1, the Space row is row 6) 5. Currently each row can have 14 keys. If you want more, you need add a nested if / else block to the custom 'setKeyboardColFromMouseX' block. If you want to add rows, use the custom 'setKeyboardRowFromMouseY' block.
Credit to @boahthenoah for his Josh sprite, me for the backdrop.