keyEvents.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. window.location = expeUrl + params
  53. }
  54. }
  55. }