keyEvents.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Utils informations
  2. const KEYCODE_Q = 81
  3. const KEYCODE_ENTER = 13
  4. const KEYCODE_LEFT_ARROW = 37
  5. const KEYCODE_RIGHT_ARROW = 39
  6. urlParams = new URLSearchParams(window.location.search)
  7. const scene = urlParams.get('scene')
  8. const expe = urlParams.get('expe')
  9. const checkKey = e => {
  10. if (e.keyCode === KEYCODE_Q) {
  11. // `q` to quit expe
  12. console.log('`q` key is pressed')
  13. window.location = baseUrl
  14. }
  15. else if (e.keyCode === KEYCODE_ENTER) {
  16. // check if experiments is begin
  17. if (!BEGIN_EXPE) {
  18. // right arrow
  19. window.location = window.location.href + '&begin=true'
  20. }
  21. }
  22. else if (e.keyCode === KEYCODE_LEFT_ARROW || e.keyCode === KEYCODE_RIGHT_ARROW) {
  23. // only do something is experiments has begun
  24. if (BEGIN_EXPE) {
  25. let answer
  26. // left arrow key
  27. if (e.keyCode === KEYCODE_LEFT_ARROW) {
  28. console.log('left arrow is pressed')
  29. answer = '1'
  30. }
  31. // right arrow key
  32. if (e.keyCode === KEYCODE_RIGHT_ARROW) {
  33. console.log('right arrow is pressed')
  34. answer = '0'
  35. }
  36. let iteration = 0
  37. // update of iteration if exists
  38. if (urlParams.has('iteration')) {
  39. iteration = urlParams.get('iteration')
  40. // increment step
  41. iteration++
  42. }
  43. // construct url with params for experiments
  44. const params = `?scene=${scene}&expe=${expe}&iteration=${iteration}&answer=${answer}`
  45. window.location = expeUrl + params
  46. }
  47. }
  48. }
  49. // implement `key` events
  50. document.addEventListener('keydown', checkKey)
  51. // avoid back button return 30 times... (Need to improve this..)
  52. for (var i = 0; i < 30; i++){
  53. window.history.pushState({isBackPage: false, }, document.title, location.href)
  54. }