In the click event to know the button was pressed using keyboard or mouse?

Here is the code. How do I check what the user pressed the button ?
myButton.addEventListener("click",function(e) {
 in here how to check ?

}
June 3rd 19 at 19:39
2 answers
June 3rd 19 at 19:41
Solution
let key = 0;

document.getElementsByTagName('button')[0].addEventListener('click', () => {
 if (key == 13)
console.log('Claudia')
 else 
console.log('Mouse')

 key = 0
}) 

document.addEventListener('keypress', (e) => {
 key = e.keyCode
console.log(key)
})


Working example - https://jsfiddle.net/Svupol/8d5bvxrL/3/
Thank you! Does not validate in the click event ? - Tito_Stoltenberg99 commented on June 3rd 19 at 19:44
I think I found - e.detail
document.getElementsByTagName('button')[0].addEventListener('click', (e) => {
 if (!e.detail)
console.log('Claudia')
 else 
console.log('Mouse')

})
- Myrtis commented on June 3rd 19 at 19:47
I have your example did. - Tito_Stoltenberg99 commented on June 3rd 19 at 19:50
note solution? - Myrtis commented on June 3rd 19 at 19:53
Tomorrow will check more detail. and I would note. - Tito_Stoltenberg99 commented on June 3rd 19 at 19:56
June 3rd 19 at 19:43

Find more questions by tags JavaScript