keyEvents.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // implement `key` events
  2. document.onkeydown = checkKey;
  3. urlParams = new URLSearchParams(window.location.search);
  4. // Utils informations
  5. var KEYCODE_Q = '81'
  6. var KEYCODE_ENTER = '13'
  7. var KEYCODE_LEFT_ARROW = '37'
  8. var KEYCODE_RIGHT_ARROW = '39'
  9. // get params if exists
  10. if (urlParams.has('scene')){
  11. var scene = urlParams.get('scene')
  12. var expe = urlParams.get('expe')
  13. }
  14. function checkKey(e) {
  15. e = e || window.event;
  16. if (e.keyCode == '81') {
  17. // `q` for quit expe
  18. console.log('`q` key is pressed')
  19. window.location = baseUrl
  20. }
  21. else if (e.keyCode == '13') {
  22. // check if experience is begin
  23. if (!BEGIN_EXPE){
  24. console.log(window.location.href + "&begin=true")
  25. // right arrow
  26. window.location = window.location.href + "&begin=true"
  27. }
  28. }
  29. else if (e.keyCode == '37' || e.keyCode == '39'){
  30. // only do something is experience is begin
  31. if (BEGIN_EXPE){
  32. var answer;
  33. // left arrow key
  34. if (e.keyCode == '37'){
  35. console.log('left arrow is pressed')
  36. answer = '1'
  37. }
  38. // right arrow key
  39. if (e.keyCode == '39'){
  40. console.log('right arrow is pressed')
  41. answer = '0'
  42. }
  43. var iteration = 0;
  44. // update of iteration if exists
  45. if (urlParams.has('iteration')){
  46. iteration = urlParams.get('iteration')
  47. // increment step
  48. iteration++;
  49. }
  50. // construct url with params for experience
  51. var params = "?scene=" + scene + "&expe=" + expe + "&iteration=" + iteration + "&answer=" + answer
  52. console.log(expeUrl + params)
  53. window.location = expeUrl + params
  54. }
  55. }
  56. }