Although I found the answers to this question, they were for jQuery and not suitable for my needs with vue.js.
Currently, I have successfully implemented code to detect single character presses:
export default {
name: 'game',
data () {
return {
allowInput: false,
disabledKeys: ['ArrowLeft', 'Home', 'Control']
}
},
methods: {
keymonitor: function (event) {
console.log(event.key)
if (this.disabledKeys.indexOf(event.key) >= 0) {
event.preventDefault()
this.allowInput = false
// alert('not allowed')
} else {
this.allowInput = true
}
},
checkAnswer () {
if (! this.allowInput) {
alert('the key(s) you pressed is/are not allowed')
}
} /* END checkAnswer */
} /* END methods */
} /* END export default */
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.2.2/vue.min.js"></script>
<input id="typeBox" ref="typeBox" autocomplete="off" placeholder="Type here..."
@keydown="keymonitor" @keyup="checkAnswer()" />
The current code successfully prevents specific key presses like ArrowLeft, Home, and Control.
However, there's an issue:
I am now trying to find a solution to detect Ctrl+V in order to prevent paste action in the text box. If anyone has any insight on how to achieve this, I would greatly appreciate it. Thank you in advance.