In einem Spiel KeyUp Event ausführen???

  • VB.NET

Es gibt 12 Antworten in diesem Thema. Der letzte Beitrag () ist von FAtheone.

    In einem Spiel KeyUp Event ausführen???

    Hay habe folgendes problem:

    Ich habe ein Programm gemacht für ein Spiel. Das Programm soll halt immer wenn man "Z" drückt, F1 - F9 durchdrücken. Doch wenn ich jetz zu dem Spiel wechsle und "Z" drücke passiert nix.
    [Ps: Ein bissl umständlich gecodet]

    VB.NET-Quellcode

    1. Public Class Form1
    2. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    3. Me.KeyPreview = True
    4. End Sub
    5. Private Sub Form1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
    6. If e.KeyCode = Keys.Z Then
    7. Button1.PerformClick()
    8. ElseIf e.KeyCode = Keys.Q Then
    9. Button2.PerformClick()
    10. End If
    11. End Sub
    12. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    13. If ComboBox1.Text = "F - Leiste 1" Then
    14. SendKeys.Send(Keys.D1)
    15. SendKeys.Send(Keys.F1)
    16. SendKeys.Send(Keys.F2)
    17. SendKeys.Send(Keys.F3)
    18. SendKeys.Send(Keys.F4)
    19. SendKeys.Send(Keys.F5)
    20. SendKeys.Send(Keys.F6)
    21. SendKeys.Send(Keys.F7)
    22. SendKeys.Send(Keys.F8)
    23. SendKeys.Send(Keys.F9)
    24. ElseIf ComboBox1.Text = "F - Leiste 2" Then
    25. SendKeys.Send(Keys.D2)
    26. SendKeys.Send(Keys.F1)
    27. SendKeys.Send(Keys.F2)
    28. SendKeys.Send(Keys.F3)
    29. SendKeys.Send(Keys.F4)
    30. SendKeys.Send(Keys.F5)
    31. SendKeys.Send(Keys.F6)
    32. SendKeys.Send(Keys.F7)
    33. SendKeys.Send(Keys.F8)
    34. SendKeys.Send(Keys.F9)
    35. ElseIf ComboBox1.Text = "F - Leiste 3" Then
    36. SendKeys.Send(Keys.D3)
    37. SendKeys.Send(Keys.F1)
    38. SendKeys.Send(Keys.F2)
    39. SendKeys.Send(Keys.F3)
    40. SendKeys.Send(Keys.F4)
    41. SendKeys.Send(Keys.F5)
    42. SendKeys.Send(Keys.F6)
    43. SendKeys.Send(Keys.F7)
    44. SendKeys.Send(Keys.F8)
    45. SendKeys.Send(Keys.F9)
    46. ElseIf ComboBox1.Text = "F - Leiste 4" Then
    47. SendKeys.Send(Keys.D4)
    48. SendKeys.Send(Keys.F1)
    49. SendKeys.Send(Keys.F2)
    50. SendKeys.Send(Keys.F3)
    51. SendKeys.Send(Keys.F4)
    52. SendKeys.Send(Keys.F5)
    53. SendKeys.Send(Keys.F6)
    54. SendKeys.Send(Keys.F7)
    55. SendKeys.Send(Keys.F8)
    56. SendKeys.Send(Keys.F9)
    57. ElseIf ComboBox1.Text = "F - Leiste 5" Then
    58. SendKeys.Send(Keys.D5)
    59. SendKeys.Send(Keys.F1)
    60. SendKeys.Send(Keys.F2)
    61. SendKeys.Send(Keys.F3)
    62. SendKeys.Send(Keys.F4)
    63. SendKeys.Send(Keys.F5)
    64. SendKeys.Send(Keys.F6)
    65. SendKeys.Send(Keys.F7)
    66. SendKeys.Send(Keys.F8)
    67. SendKeys.Send(Keys.F9)
    68. ElseIf ComboBox1.Text = "F - Leiste 6" Then
    69. SendKeys.Send(Keys.D6)
    70. SendKeys.Send(Keys.F1)
    71. SendKeys.Send(Keys.F2)
    72. SendKeys.Send(Keys.F3)
    73. SendKeys.Send(Keys.F4)
    74. SendKeys.Send(Keys.F5)
    75. SendKeys.Send(Keys.F6)
    76. SendKeys.Send(Keys.F7)
    77. SendKeys.Send(Keys.F8)
    78. SendKeys.Send(Keys.F9)
    79. ElseIf ComboBox1.Text = "F - Leiste 7" Then
    80. SendKeys.Send(Keys.D7)
    81. SendKeys.Send(Keys.F1)
    82. SendKeys.Send(Keys.F2)
    83. SendKeys.Send(Keys.F3)
    84. SendKeys.Send(Keys.F4)
    85. SendKeys.Send(Keys.F5)
    86. SendKeys.Send(Keys.F6)
    87. SendKeys.Send(Keys.F7)
    88. SendKeys.Send(Keys.F8)
    89. SendKeys.Send(Keys.F9)
    90. ElseIf ComboBox1.Text = "F - Leiste 8" Then
    91. SendKeys.Send(Keys.D8)
    92. SendKeys.Send(Keys.F1)
    93. SendKeys.Send(Keys.F2)
    94. SendKeys.Send(Keys.F3)
    95. SendKeys.Send(Keys.F4)
    96. SendKeys.Send(Keys.F5)
    97. SendKeys.Send(Keys.F6)
    98. SendKeys.Send(Keys.F7)
    99. SendKeys.Send(Keys.F8)
    100. SendKeys.Send(Keys.F9)
    101. ElseIf ComboBox1.Text = "F - Leiste 9" Then
    102. SendKeys.Send(Keys.D9)
    103. SendKeys.Send(Keys.F1)
    104. SendKeys.Send(Keys.F2)
    105. SendKeys.Send(Keys.F3)
    106. SendKeys.Send(Keys.F4)
    107. SendKeys.Send(Keys.F5)
    108. SendKeys.Send(Keys.F6)
    109. SendKeys.Send(Keys.F7)
    110. SendKeys.Send(Keys.F8)
    111. SendKeys.Send(Keys.F9)
    112. End If
    113. End Sub
    114. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    115. If ComboBox2.Text = "F - Leiste 1" Then
    116. SendKeys.Send(Keys.D1)
    117. SendKeys.Send(Keys.F1)
    118. SendKeys.Send(Keys.F2)
    119. SendKeys.Send(Keys.F3)
    120. SendKeys.Send(Keys.F4)
    121. SendKeys.Send(Keys.F5)
    122. SendKeys.Send(Keys.F6)
    123. SendKeys.Send(Keys.F7)
    124. SendKeys.Send(Keys.F8)
    125. SendKeys.Send(Keys.F9)
    126. ElseIf ComboBox2.Text = "F - Leiste 2" Then
    127. SendKeys.Send(Keys.D2)
    128. SendKeys.Send(Keys.F1)
    129. SendKeys.Send(Keys.F2)
    130. SendKeys.Send(Keys.F3)
    131. SendKeys.Send(Keys.F4)
    132. SendKeys.Send(Keys.F5)
    133. SendKeys.Send(Keys.F6)
    134. SendKeys.Send(Keys.F7)
    135. SendKeys.Send(Keys.F8)
    136. SendKeys.Send(Keys.F9)
    137. ElseIf ComboBox2.Text = "F - Leiste 3" Then
    138. SendKeys.Send(Keys.D3)
    139. SendKeys.Send(Keys.F1)
    140. SendKeys.Send(Keys.F2)
    141. SendKeys.Send(Keys.F3)
    142. SendKeys.Send(Keys.F4)
    143. SendKeys.Send(Keys.F5)
    144. SendKeys.Send(Keys.F6)
    145. SendKeys.Send(Keys.F7)
    146. SendKeys.Send(Keys.F8)
    147. SendKeys.Send(Keys.F9)
    148. ElseIf ComboBox2.Text = "F - Leiste 4" Then
    149. SendKeys.Send(Keys.D4)
    150. SendKeys.Send(Keys.F1)
    151. SendKeys.Send(Keys.F2)
    152. SendKeys.Send(Keys.F3)
    153. SendKeys.Send(Keys.F4)
    154. SendKeys.Send(Keys.F5)
    155. SendKeys.Send(Keys.F6)
    156. SendKeys.Send(Keys.F7)
    157. SendKeys.Send(Keys.F8)
    158. SendKeys.Send(Keys.F9)
    159. ElseIf ComboBox2.Text = "F - Leiste 5" Then
    160. SendKeys.Send(Keys.D5)
    161. SendKeys.Send(Keys.F1)
    162. SendKeys.Send(Keys.F2)
    163. SendKeys.Send(Keys.F3)
    164. SendKeys.Send(Keys.F4)
    165. SendKeys.Send(Keys.F5)
    166. SendKeys.Send(Keys.F6)
    167. SendKeys.Send(Keys.F7)
    168. SendKeys.Send(Keys.F8)
    169. SendKeys.Send(Keys.F9)
    170. ElseIf ComboBox2.Text = "F - Leiste 6" Then
    171. SendKeys.Send(Keys.D6)
    172. SendKeys.Send(Keys.F1)
    173. SendKeys.Send(Keys.F2)
    174. SendKeys.Send(Keys.F3)
    175. SendKeys.Send(Keys.F4)
    176. SendKeys.Send(Keys.F5)
    177. SendKeys.Send(Keys.F6)
    178. SendKeys.Send(Keys.F7)
    179. SendKeys.Send(Keys.F8)
    180. SendKeys.Send(Keys.F9)
    181. ElseIf ComboBox2.Text = "F - Leiste 7" Then
    182. SendKeys.Send(Keys.D7)
    183. SendKeys.Send(Keys.F1)
    184. SendKeys.Send(Keys.F2)
    185. SendKeys.Send(Keys.F3)
    186. SendKeys.Send(Keys.F4)
    187. SendKeys.Send(Keys.F5)
    188. SendKeys.Send(Keys.F6)
    189. SendKeys.Send(Keys.F7)
    190. SendKeys.Send(Keys.F8)
    191. SendKeys.Send(Keys.F9)
    192. ElseIf ComboBox2.Text = "F - Leiste 8" Then
    193. SendKeys.Send(Keys.D8)
    194. SendKeys.Send(Keys.F1)
    195. SendKeys.Send(Keys.F2)
    196. SendKeys.Send(Keys.F3)
    197. SendKeys.Send(Keys.F4)
    198. SendKeys.Send(Keys.F5)
    199. SendKeys.Send(Keys.F6)
    200. SendKeys.Send(Keys.F7)
    201. SendKeys.Send(Keys.F8)
    202. SendKeys.Send(Keys.F9)
    203. ElseIf ComboBox2.Text = "F - Leiste 9" Then
    204. SendKeys.Send(Keys.D9)
    205. SendKeys.Send(Keys.F1)
    206. SendKeys.Send(Keys.F2)
    207. SendKeys.Send(Keys.F3)
    208. SendKeys.Send(Keys.F4)
    209. SendKeys.Send(Keys.F5)
    210. SendKeys.Send(Keys.F6)
    211. SendKeys.Send(Keys.F7)
    212. SendKeys.Send(Keys.F8)
    213. SendKeys.Send(Keys.F9)
    214. End If
    215. End Sub
    216. End Class
    Hab jetz nochmal umgeschrieben, aber wenn ioch jetz "Z" drücke, sollte es eig F1 bis F9 durchdrücken, aber das passiert nicht sondern es kommt diese Fehlermeldung:

    SendKeys kann nicht innerhalb der Anwendung ausgeführt werden, da diese Anwendung keine Windows-Meldungen verarbeitet. Ändern Sie die Anwendung so, dass sie Meldungen behandelt, oder verwenden Sie die SendKeys.SendWait-Methode.

    Der Code soeht jetz so aus:

    VB.NET-Quellcode

    1. Public Class Form1
    2. Private Declare Function GetAsyncKeyState Lib "User32" (ByVal vKey As Integer) As Short
    3. Function GetTaste(ByVal Taste As Integer) As Boolean
    4. If GetAsyncKeyState(Keys.Z) = -32767 Then
    5. Return True
    6. End If
    7. End Function
    8. Dim hotkeyThread As New Threading.Thread(AddressOf CheckKeyPressed)
    9. Sub CheckKeyPressed()
    10. Do
    11. If GetTaste(Keys.Z) = True Then
    12. SendKeys.Send(Keys.F1)
    13. SendKeys.Send(Keys.F2)
    14. SendKeys.Send(Keys.F3)
    15. SendKeys.Send(Keys.F4)
    16. SendKeys.Send(Keys.F5)
    17. SendKeys.Send(Keys.F6)
    18. SendKeys.Send(Keys.F7)
    19. SendKeys.Send(Keys.F8)
    20. SendKeys.Send(Keys.F9)
    21. End If
    22. Loop
    23. End Sub
    24. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    25. hotkeyThread.Start()
    26. Me.KeyPreview = True
    27. End Sub
    28. End Class