This seems like a bad idea, as there is a lot of extra text and text input that provides a text box for free (carriage, select, cut, paste, drag and drop, manipulate arrow keys, etc.), but here are two things you need to do:
- Give the attribute
<canvas> a tabindex so that it can receive focus and therefore increase key events; - Add a
keypress (not keydown ) element to the <canvas> to enter text.
the code:
<canvas id="textarea" tabindex="1" width="300" height="200"></canvas> <script type="text/javascript"> var el = document.getElementById("textarea"); el.onkeypress = function(evt) { var charCode = evt.which; var charStr = String.fromCharCode(charCode); alert(charStr); }; </script>
Tim down
source share