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. console.log("Here")
  23. // check if experience is begin
  24. if (!BEGIN_EXPE){
  25. console.log("And Here")
  26. // right arrow
  27. window.location = window.location.href + "&begin=true"
  28. }
  29. }
  30. else if (e.keyCode == '37' || e.keyCode == '39'){
  31. // only do something is experience is begin
  32. if (BEGIN_EXPE){
  33. var answer;
  34. // left arrow key
  35. if (e.keyCode == '37'){
  36. console.log('left arrow is pressed')
  37. answer = '1'
  38. }
  39. // right arrow key
  40. if (e.keyCode == '39'){
  41. console.log('right arrow is pressed')
  42. answer = '0'
  43. }
  44. var iteration = 0;
  45. // update of iteration if exists
  46. if (urlParams.has('iteration')){
  47. iteration = urlParams.get('iteration')
  48. // increment step
  49. iteration++;
  50. }
  51. // construct url with params for experience
  52. var params = "?scene=" + scene + "&expe=" + expe + "&iteration=" + iteration + "&answer=" + answer
  53. window.location = baseExpeUrl + params
  54. }
  55. }
  56. }