PageRenderTime 45ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/docs/scripts/WinLIRC.htm

http://autohotkey-chinese.googlecode.com/
HTML | 298 lines | 257 code | 41 blank | 0 comment | 0 complexity | 54b6427fb38307dfcd765d2708218306 MD5 | raw file
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/html4/loose.dtd">
  3. <html>
  4. <head>
  5. <title>WinLIRC Client</title>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <link rel="alternate" type="application/rss+xml" title="AutoHotkey Forum RSS" href="/forum/rss.php">
  8. <link href="/docs/css/default.css" rel="stylesheet" type="text/css">
  9. <link href="/docs/css/print.css" rel="stylesheet" type="text/css" media="print">
  10. <meta name="description" content="This open-source WinLIRC client can perform keystrokes, mouse clicks, and other actions in response to buttons you press on your remote control.">
  11. </head>
  12. <body>
  13. <h6>WinLIRC Client</h6>
  14. <p> This script receives notifications from <a href="http://winlirc.sourceforge.net">WinLIRC</a> whenever you press
  15. a button on your remote control. It can be used to automate Winamp,
  16. Windows Media Player, etc. It's easy to configure. For example, if
  17. WinLIRC recognizes a button named &quot;VolUp&quot; on your remote control,
  18. create a label named VolUp and beneath it use the command
  19. &quot;SoundSet +5&quot; to increase the soundcard's volume by 5%.
  20. </p>
  21. <p><a href="WinLIRC.ahk">Download This Script</a> &nbsp;| &nbsp;<a href="index.htm">Other Sample Scripts</a> &nbsp;| &nbsp;<a href="http://www.autohotkey.com">Home</a></p>
  22. <pre class="NoIndent"><em>; Here are the steps to use this script:</em>
  23. <em>; 1) Configure WinLIRC to recognize your remote control and its buttons.</em>
  24. <em>; WinLIRC is at <a href="http://winlirc.sourceforge.net">http://winlirc.sourceforge.net</a></em>
  25. <em>; 2) Edit the WinLIRC path, address, and port in the CONFIG section below.</em>
  26. <em>; 3) Launch this script. It will start the WinLIRC server if needed.</em>
  27. <em>; 4) Press some buttons on your remote control. A small window will</em>
  28. <em>; appear showing the name of each button as you press it.</em>
  29. <em>; 5) Configure your buttons to send keystrokes and mouse clicks to</em>
  30. <em>; windows such as Winamp, Media Player, etc. See the examples below.</em>
  31. <em>; This script requires AutoHotkey 1.0.38.04 or later.</em>
  32. <em>; HISTORY OF CHANGES</em>
  33. <em>; March 2, 2007:</em>
  34. <em>; - Improved reliability via &quot;Critical&quot; in ReceiveData().</em>
  35. <em>; October 5, 2005:</em>
  36. <em>; - Eliminated Winsock warning dialog &quot;10054&quot; upon system shutdown/logoff.</em>
  37. <em>; - Added option &quot;DelayBetweenButtonRepeats&quot; to throttle the repeat speed.</em>
  38. <em>; -------------------------------------------------</em>
  39. <em>; CONFIGURATION SECTION: Set your preferences here.</em>
  40. <em>; -------------------------------------------------</em>
  41. <em>; Some remote controls repeat the signal rapidly while you're holding down</em>
  42. <em>; a button. This makes it difficult to get the remote to send only a single</em>
  43. <em>; signal. The following setting solves this by ignoring repeated signals</em>
  44. <em>; until the specified time has passed. 200 is often a good setting. Set it</em>
  45. <em>; to 0 to disable this feature.</em>
  46. DelayBetweenButtonRepeats = 200
  47. <em>; Specify the path to WinLIRC, such as C:\WinLIRC\winlirc.exe</em>
  48. WinLIRC_Path = %A_ProgramFiles%\WinLIRC\winlirc.exe
  49. <em>; Specify WinLIRC's address and port. The most common are 127.0.0.1 (localhost) and 8765.</em>
  50. WinLIRC_Address = 127.0.0.1
  51. WinLIRC_Port = 8765
  52. <em>; Do not change the following two lines. Skip them and continue below.</em>
  53. Gosub WinLIRC_Init
  54. return
  55. <em>; --------------------------------------------</em>
  56. <em>; ASSIGN ACTIONS TO THE BUTTONS ON YOUR REMOTE</em>
  57. <em>; --------------------------------------------</em>
  58. <em>; Configure your remote control's buttons below. Use WinLIRC's names</em>
  59. <em>; for the buttons, which can be seen in your WinLIRC config file</em>
  60. <em>; (.cf file) -- or you can press any button on your remote and the</em>
  61. <em>; script will briefly display the button's name in a small window.</em>
  62. <em>; </em>
  63. <em>; Below are some examples. Feel free to revise or delete them to suit</em>
  64. <em>; your preferences.</em>
  65. VolUp:
  66. SoundSet +5 <em>; Increase master volume by 5%. On Vista, replace this line with: Send {Volume_Up}</em>
  67. return
  68. VolDown:
  69. SoundSet -5 <em>; Reduce master volume by 5%. On Vista, replace this line with: Send {Volume_Down}</em>
  70. return
  71. ChUp:
  72. WinGetClass, ActiveClass, A
  73. if ActiveClass in Winamp v1.x,Winamp PE <em>; Winamp is active.</em>
  74. Send {right} <em>; Send a right-arrow keystroke.</em>
  75. else <em>; Some other type of window is active.</em>
  76. Send {WheelUp} <em>; Rotate the mouse wheel up by one notch.</em>
  77. return
  78. ChDown:
  79. WinGetClass, ActiveClass, A
  80. if ActiveClass in Winamp v1.x,Winamp PE <em>; Winamp is active.</em>
  81. Send {left} <em>; Send a left-arrow keystroke.</em>
  82. else <em>; Some other type of window is active.</em>
  83. Send {WheelDown} <em>; Rotate the mouse wheel down by one notch.</em>
  84. return
  85. Menu:
  86. IfWinExist, Untitled - Notepad
  87. {
  88. WinActivate
  89. }
  90. else
  91. {
  92. Run, Notepad
  93. WinWait, Untitled - Notepad
  94. WinActivate
  95. }
  96. Send Here are some keystrokes sent to Notepad.{Enter}
  97. return
  98. <em>; The examples above give a feel for how to accomplish common tasks.</em>
  99. <em>; To learn the basics of AutoHotkey, check out the Quick-start Tutorial</em>
  100. <em>; at <a href="http://www.autohotkey.com/docs/Tutorial.htm">http://www.autohotkey.com/docs/Tutorial.htm</a></em>
  101. <em>; ----------------------------</em>
  102. <em>; END OF CONFIGURATION SECTION</em>
  103. <em>; ----------------------------</em>
  104. <em>; Do not make changes below this point unless you want to change the core</em>
  105. <em>; functionality of the script.</em>
  106. WinLIRC_Init:
  107. OnExit, ExitSub <em>; For connection cleanup purposes.</em>
  108. <em>; Launch WinLIRC if it isn't already running:</em>
  109. Process, Exist, winlirc.exe
  110. if not ErrorLevel <em>; No PID for WinLIRC was found.</em>
  111. {
  112. IfNotExist, %WinLIRC_Path%
  113. {
  114. MsgBox The file &quot;%WinLIRC_Path%&quot; does not exist. Please edit this script to specify its location.
  115. ExitApp
  116. }
  117. Run %WinLIRC_Path%
  118. Sleep 200 <em>; Give WinLIRC a little time to initialize (probably never needed, just for peace of mind).</em>
  119. }
  120. <em>; Connect to WinLIRC (or any type of server for that matter):</em>
  121. socket := ConnectToAddress(WinLIRC_Address, WinLIRC_Port)
  122. if socket = -1 <em>; Connection failed (it already displayed the reason).</em>
  123. ExitApp
  124. <em>; Find this script's main window:</em>
  125. Process, Exist <em>; This sets ErrorLevel to this script's PID (it's done this way to support compiled scripts).</em>
  126. DetectHiddenWindows On
  127. ScriptMainWindowId := WinExist(&quot;ahk_class AutoHotkey ahk_pid &quot; . ErrorLevel)
  128. DetectHiddenWindows Off
  129. <em>; When the OS notifies the script that there is incoming data waiting to be received,</em>
  130. <em>; the following causes a function to be launched to read the data:</em>
  131. NotificationMsg = 0x5555 <em>; An arbitrary message number, but should be greater than 0x1000.</em>
  132. OnMessage(NotificationMsg, &quot;ReceiveData&quot;)
  133. <em>; Set up the connection to notify this script via message whenever new data has arrived.</em>
  134. <em>; This avoids the need to poll the connection and thus cuts down on resource usage.</em>
  135. FD_READ = 1 <em>; Received when data is available to be read.</em>
  136. FD_CLOSE = 32 <em>; Received when connection has been closed.</em>
  137. if DllCall(&quot;Ws2_32\WSAAsyncSelect&quot;, &quot;UInt&quot;, socket, &quot;UInt&quot;, ScriptMainWindowId, &quot;UInt&quot;, NotificationMsg, &quot;Int&quot;, FD_READ|FD_CLOSE)
  138. {
  139. MsgBox % &quot;WSAAsyncSelect() indicated Winsock error &quot; . DllCall(&quot;Ws2_32\WSAGetLastError&quot;)
  140. ExitApp
  141. }
  142. return
  143. ConnectToAddress(IPAddress, Port)
  144. <em>; This can connect to most types of TCP servers, not just WinLIRC.</em>
  145. <em>; Returns -1 (INVALID_SOCKET) upon failure or the socket ID upon success.</em>
  146. {
  147. VarSetCapacity(wsaData, 32) <em>; The struct is only about 14 in size, so 32 is conservative.</em>
  148. result := DllCall(&quot;Ws2_32\WSAStartup&quot;, &quot;UShort&quot;, 0x0002, &quot;UInt&quot;, &amp;wsaData) <em>; Request Winsock 2.0 (0x0002)</em>
  149. <em>; Since WSAStartup() will likely be the first Winsock function called by this script,</em>
  150. <em>; check ErrorLevel to see if the OS has Winsock 2.0 available:</em>
  151. if ErrorLevel
  152. {
  153. MsgBox WSAStartup() could not be called due to error %ErrorLevel%. Winsock 2.0 or higher is required.
  154. return -1
  155. }
  156. if result <em>; Non-zero, which means it failed (most Winsock functions return 0 upon success).</em>
  157. {
  158. MsgBox % &quot;WSAStartup() indicated Winsock error &quot; . DllCall(&quot;Ws2_32\WSAGetLastError&quot;)
  159. return -1
  160. }
  161. AF_INET = 2
  162. SOCK_STREAM = 1
  163. IPPROTO_TCP = 6
  164. socket := DllCall(&quot;Ws2_32\socket&quot;, &quot;Int&quot;, AF_INET, &quot;Int&quot;, SOCK_STREAM, &quot;Int&quot;, IPPROTO_TCP)
  165. if socket = -1
  166. {
  167. MsgBox % &quot;socket() indicated Winsock error &quot; . DllCall(&quot;Ws2_32\WSAGetLastError&quot;)
  168. return -1
  169. }
  170. <em>; Prepare for connection:</em>
  171. SizeOfSocketAddress = 16
  172. VarSetCapacity(SocketAddress, SizeOfSocketAddress)
  173. InsertInteger(2, SocketAddress, 0, AF_INET) <em>; sin_family</em>
  174. InsertInteger(DllCall(&quot;Ws2_32\htons&quot;, &quot;UShort&quot;, Port), SocketAddress, 2, 2) <em>; sin_port</em>
  175. InsertInteger(DllCall(&quot;Ws2_32\inet_addr&quot;, &quot;Str&quot;, IPAddress), SocketAddress, 4, 4) <em>; sin_addr.s_addr</em>
  176. <em>; Attempt connection:</em>
  177. if DllCall(&quot;Ws2_32\connect&quot;, &quot;UInt&quot;, socket, &quot;UInt&quot;, &amp;SocketAddress, &quot;Int&quot;, SizeOfSocketAddress)
  178. {
  179. MsgBox % &quot;connect() indicated Winsock error &quot; . DllCall(&quot;Ws2_32\WSAGetLastError&quot;) . &quot;. Is WinLIRC running?&quot;
  180. return -1
  181. }
  182. return socket <em>; Indicate success by returning a valid socket ID rather than -1.</em>
  183. }
  184. ReceiveData(wParam, lParam)
  185. <em>; By means of OnMessage(), this function has been set up to be called automatically whenever new data</em>
  186. <em>; arrives on the connection. It reads the data from WinLIRC and takes appropriate action depending</em>
  187. <em>; on the contents.</em>
  188. {
  189. Critical <em>; Prevents another of the same message from being discarded due to thread-already-running.</em>
  190. socket := wParam
  191. ReceivedDataSize = 4096 <em>; Large in case a lot of data gets buffered due to delay in processing previous data.</em>
  192. VarSetCapacity(ReceivedData, ReceivedDataSize, 0) <em>; 0 for last param terminates string for use with recv().</em>
  193. ReceivedDataLength := DllCall(&quot;Ws2_32\recv&quot;, &quot;UInt&quot;, socket, &quot;Str&quot;, ReceivedData, &quot;Int&quot;, ReceivedDataSize, &quot;Int&quot;, 0)
  194. if ReceivedDataLength = 0 <em>; The connection was gracefully closed, probably due to exiting WinLIRC.</em>
  195. ExitApp <em>; The OnExit routine will call WSACleanup() for us.</em>
  196. if ReceivedDataLength = -1
  197. {
  198. WinsockError := DllCall(&quot;Ws2_32\WSAGetLastError&quot;)
  199. if WinsockError = 10035 <em>; WSAEWOULDBLOCK, which means &quot;no more data to be read&quot;.</em>
  200. return 1
  201. if WinsockError &lt;&gt; 10054 <em>; WSAECONNRESET, which happens when WinLIRC closes via system shutdown/logoff.</em>
  202. <em>; Since it's an unexpected error, report it. Also exit to avoid infinite loop.</em>
  203. MsgBox % &quot;recv() indicated Winsock error &quot; . WinsockError
  204. ExitApp <em>; The OnExit routine will call WSACleanup() for us.</em>
  205. }
  206. <em>; Otherwise, process the data received. Testing shows that it's possible to get more than one line</em>
  207. <em>; at a time (even for explicitly-sent IR signals), which the following method handles properly.</em>
  208. <em>; Data received from WinLIRC looks like the following example (see the WinLIRC docs for details):</em>
  209. <em>; 0000000000eab154 00 NameOfButton NameOfRemote</em>
  210. Loop, parse, ReceivedData, `n, `r
  211. {
  212. if A_LoopField in ,BEGIN,SIGHUP,END <em>; Ignore blank lines and WinLIRC's start-up messages.</em>
  213. continue
  214. ButtonName = <em>; Init to blank in case there are less than 3 fields found below.</em>
  215. Loop, parse, A_LoopField, %A_Space% <em>; Extract the button name, which is the third field.</em>
  216. if A_Index = 3
  217. ButtonName := A_LoopField
  218. global DelayBetweenButtonRepeats <em>; Declare globals to make them available to this function.</em>
  219. static PrevButtonName, PrevButtonTime, RepeatCount <em>; These variables remember their values between calls.</em>
  220. if (ButtonName != PrevButtonName || A_TickCount - PrevButtonTime &gt; DelayBetweenButtonRepeats)
  221. {
  222. if IsLabel(ButtonName) <em>; There is a subroutine associated with this button.</em>
  223. Gosub %ButtonName% <em>; Launch the subroutine.</em>
  224. else <em>; Since there is no associated subroutine, briefly display which button was pressed.</em>
  225. {
  226. if (ButtonName == PrevButtonName)
  227. RepeatCount += 1
  228. else
  229. RepeatCount = 1
  230. SplashTextOn, 150, 20, Button from WinLIRC, %ButtonName% (%RepeatCount%)
  231. SetTimer, SplashOff, 3000 <em>; This allows more signals to be processed while displaying the window.</em>
  232. }
  233. PrevButtonName := ButtonName
  234. PrevButtonTime := A_TickCount
  235. }
  236. }
  237. return 1 <em>; Tell the program that no further processing of this message is needed.</em>
  238. }
  239. SplashOff:
  240. SplashTextOff
  241. SetTimer, SplashOff, Off
  242. return
  243. InsertInteger(pInteger, ByRef pDest, pOffset = 0, pSize = 4)
  244. <em>; The caller must ensure that pDest has sufficient capacity. To preserve any existing contents in pDest,</em>
  245. <em>; only pSize number of bytes starting at pOffset are altered in it.</em>
  246. {
  247. Loop %pSize% <em>; Copy each byte in the integer into the structure as raw binary data.</em>
  248. DllCall(&quot;RtlFillMemory&quot;, &quot;UInt&quot;, &amp;pDest + pOffset + A_Index-1, &quot;UInt&quot;, 1, &quot;UChar&quot;, pInteger &gt;&gt; 8*(A_Index-1) &amp; 0xFF)
  249. }
  250. ExitSub: <em>; This subroutine is called automatically when the script exits for any reason.</em>
  251. <em>; MSDN: &quot;Any sockets open when WSACleanup is called are reset and automatically</em>
  252. <em>; deallocated as if closesocket was called.&quot;</em>
  253. DllCall(&quot;Ws2_32\WSACleanup&quot;)
  254. ExitApp
  255. </pre>
  256. </body>
  257. </html>