Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.35 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Linq;
  5. using System;
  6. using System.Globalization;
  7.  
  8. public class KeyLogger : MonoBehaviour
  9. {
  10. readonly char[] smallLetters = "abcdefghjkmnopqrstuvwxyz".ToCharArray();
  11. readonly char[] capitalLetters = "ABCDEFGHJKMNOPQRSTUVWXYZ".ToCharArray();
  12. readonly char[] numbers = "1234567890".ToCharArray();
  13. ExperimentControl control;
  14. char selectedKey = new char();
  15. KeyCode anyKey;
  16. KeyCode selectedKeyCode = KeyCode.CapsLock;
  17. List<KeyVector> KeyLogs;
  18. KeyVector currentKey;
  19.  
  20. char lastKey;
  21. float FT = 0;
  22. bool fingerFlying = false;
  23. float timeKeyPressed = 0;
  24. float lastKeyPressed = 0;
  25. int keyId = 0;
  26. int updateCount = 0;
  27. string type = "";
  28.  
  29. bool LoggerOn=false;
  30.  
  31. private void Awake()
  32. {
  33. control = GameObject.Find("ExperimentControl").GetComponent<ExperimentControl>();
  34. KeyLogs = control.data;
  35. }
  36. void OnGUI()
  37. {
  38. if (LoggerOn)
  39. {
  40. Event e = Event.current;
  41. if (e.isKey)
  42. {
  43. //Debug.Log("Detected key code: " + e.keyCode + " "+e.character);
  44. if (smallLetters.Contains(e.character))
  45. {
  46. selectedKey = e.character;
  47. type = "small";
  48. keyId++;
  49. }
  50. if (capitalLetters.Contains(e.character))
  51. {
  52. selectedKey = e.character;
  53. type = "capital";
  54. keyId++;
  55. }
  56. if (numbers.Contains(e.character))
  57. {
  58. selectedKey = e.character;
  59.  
  60. type = "num";
  61. keyId++;
  62. }
  63. if ((e.keyCode >= KeyCode.A && e.keyCode <= KeyCode.Z) || (e.keyCode >= KeyCode.Alpha0 && e.keyCode <= KeyCode.Alpha9) || (e.keyCode >= KeyCode.Keypad0 && e.keyCode <= KeyCode.Keypad9))
  64. {
  65. selectedKeyCode = e.keyCode;
  66. }
  67. anyKey = e.keyCode;
  68. }
  69.  
  70. }
  71. }
  72. void Update()
  73. {
  74. if (LoggerOn)
  75. {
  76. if (fingerFlying == true)
  77. {
  78. FT += Time.deltaTime;
  79. }
  80.  
  81. if (Input.anyKeyDown)
  82. {
  83. fingerFlying = false;
  84. currentKey = new KeyVector();
  85. timeKeyPressed += Time.deltaTime;
  86. if (Input.touchCount > 0)
  87. {
  88. currentKey.xCoordinate = Input.touches[0].position.x;
  89. currentKey.yCoordinate = Input.touches[0].position.y;
  90. currentKey.pressureDown = Input.touches[0].pressure;
  91. currentKey.sizeDown = Input.touches[0].radius;
  92. }
  93. MeasureSensors();
  94. updateCount++;
  95. currentKey.timeFT1 = FT;
  96. currentKey.pwdID = control.count;
  97. FT = 0;
  98.  
  99. }
  100. if (Input.anyKey)
  101. {
  102. MeasureSensors();
  103. updateCount++;
  104. timeKeyPressed += Time.deltaTime;
  105. }
  106. if (Input.GetKeyUp(selectedKeyCode))
  107. {
  108. fingerFlying = true;
  109. currentKey.userName = control.username;
  110. currentKey.timestamp = DateTime.Now;
  111.  
  112. currentKey.timeDT = timeKeyPressed;
  113. currentKey.keyId = keyId;
  114. if (lastKeyPressed != 0)
  115. {
  116. currentKey.timeFT2 = currentKey.timeFT1 + currentKey.timeDT;
  117. currentKey.timeFT3 = lastKeyPressed + currentKey.timeFT1;
  118. currentKey.timeFT4 = lastKeyPressed + currentKey.timeFT1 + currentKey.timeDT;
  119. currentKey.lastKey = lastKey;
  120. }
  121. currentKey.keyPressed = selectedKey;
  122. if (Input.touchCount > 0)
  123. {
  124. currentKey.pressureUp = Input.touches[0].pressure;
  125. currentKey.sizeUp = Input.touches[0].radius;
  126. }
  127. currentKey.gyroscopeX /= updateCount;
  128. currentKey.gyroscopeY /= updateCount;
  129. currentKey.gyroscopeZ /= updateCount;
  130. currentKey.accelX /= updateCount;
  131. currentKey.accelY /= updateCount;
  132. currentKey.accelZ /= updateCount;
  133. currentKey.rotX /= updateCount;
  134. currentKey.rotY /= updateCount;
  135. currentKey.rotZ /= updateCount;
  136.  
  137. lastKeyPressed = timeKeyPressed;
  138. timeKeyPressed = 0;
  139. lastKey = selectedKey;
  140. updateCount = 0;
  141. KeyLogs.Add(currentKey);
  142. Debug.Log(currentKey.keyPressed +" "+ currentKey.timeDT +" "+ currentKey.timeFT1);
  143.  
  144. }
  145. else if (Input.GetKeyUp(anyKey) && anyKey != KeyCode.None)
  146. {
  147. fingerFlying = true;
  148. timeKeyPressed = 0;
  149. currentKey = new KeyVector();
  150. updateCount = 0;
  151. }
  152.  
  153. }
  154. }
  155. public void MeasureSensors()
  156. {
  157. if (SystemInfo.supportsGyroscope) {
  158. currentKey.gyroscopeX += Input.gyro.attitude.x;
  159. currentKey.gyroscopeY += Input.gyro.attitude.y;
  160. currentKey.gyroscopeZ += Input.gyro.attitude.z;
  161. currentKey.rotX += Input.gyro.rotationRate.x;
  162. currentKey.rotY += Input.gyro.rotationRate.y;
  163. currentKey.rotZ += Input.gyro.rotationRate.z;
  164. }
  165. if (SystemInfo.supportsAccelerometer) {
  166. currentKey.accelX += Input.acceleration.x;
  167. currentKey.accelY += Input.acceleration.y;
  168. currentKey.accelZ += Input.acceleration.z;
  169. }
  170. }
  171. void Restart()
  172. {
  173. FT = 0;
  174. fingerFlying = false;
  175. timeKeyPressed = 0;
  176. lastKeyPressed = 0;
  177. keyId = 0;
  178. updateCount = 0;
  179. type = "";
  180. selectedKeyCode = KeyCode.CapsLock;
  181. }
  182. public void TurnOff()
  183. {
  184. Restart();
  185. LoggerOn = false;
  186.  
  187. }
  188. public void TurnOn()
  189. {
  190. Restart();
  191. LoggerOn = true;
  192. }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement