### Lua: Start and Wait for Timer Event Source: https://tweaked.cc/event/timer This Lua snippet demonstrates how to start a timer using `os.startTimer` and then wait for the corresponding 'timer' event to be fired using `os.pullEvent`. It ensures the correct timer has finished by comparing the returned ID. ```lua local timer_id = os.startTimer(2) local event, id repeat event, id = os.pullEvent("timer") until id == timer_id print("Timer with ID " .. id .. " was fired") ``` -------------------------------- ### Play Audio with Speaker and Handle Buffer Source: https://tweaked.cc/event/speaker_audio_empty This example demonstrates how to read audio data from a file in chunks and play it using the speaker. It includes logic to wait for the speaker's buffer to be available if it's full, using the 'speaker_audio_empty' event. ```lua local dfpwm = require("cc.audio.dfpwm") local speaker = peripheral.find("speaker") local decoder = dfpwm.make_decoder() for chunk in io.lines("data/example.dfpwm", 16 * 1024) do local buffer = decoder(chunk) while not speaker.playAudio(buffer) do os.pullEvent("speaker_audio_empty") end end ``` -------------------------------- ### websocket_success Event Details Source: https://tweaked.cc/event/websocket_success Details about the websocket_success event, including its return values and an example of its usage. ```APIDOC ## websocket_success Event ### Description The `websocket_success` event is fired when a WebSocket connection request returns successfully. This event is normally handled inside `http.websocket`, but it can still be seen when using `http.websocketAsync`. ### Return Values 1. `string`: The event name (`"websocket_success"`). 2. `string`: The URL of the site the WebSocket connected to. 3. `http.Websocket`: The handle for the WebSocket connection. ### Example ```lua local myURL = "wss://example.tweaked.cc/echo" http.websocketAsync(myURL) local event, url, handle repeat event, url, handle = os.pullEvent("websocket_success") until url == myURL print("Connected to " .. url) handle.send("Hello!") print(handle.receive()) handle.close() ``` ### See Also * **`http.websocketAsync`**: To open a WebSocket asynchronously. ``` -------------------------------- ### Save Transferred Files to Storage (Lua) Source: https://tweaked.cc/event/file_transfer This example shows how to handle the `file_transfer` event by saving each transferred file to the computer's local storage. It opens a new file in write-binary mode, writes the content of the transferred file, and then closes both file handles. ```lua local _, files = os.pullEvent("file_transfer") for _, file in ipairs(files.getFiles()) do local handle = fs.open(file.getName(), "wb") handle.write(file.readAll()) handle.close() file.close() end ``` -------------------------------- ### Mouse Scroll Event Details Source: https://tweaked.cc/event/mouse_scroll This section details the mouse_scroll event, its return values, and provides an example of how to use it. ```APIDOC ## mouse_scroll Event ### Description This event is fired when a mouse wheel is scrolled in the terminal. ### Method Event Listener (Implicit) ### Endpoint N/A (Client-side event) ### Parameters This event does not take any parameters directly, but it returns values upon being triggered. ### Return Values 1. `string`: The event name (`"mouse_scroll"`). 2. `number`: The direction of the scroll. (`-1` for up, `1` for down). 3. `number`: The X-coordinate of the mouse pointer when the scroll occurred. 4. `number`: The Y-coordinate of the mouse pointer when the scroll occurred. ### Request Example ```lua while true do local event, dir, x, y = os.pullEvent("mouse_scroll") print(("The mouse was scrolled in direction %s at %d, %d"):format(dir, x, y)) end ``` ### Response #### Event Trigger - **event** (string) - The name of the event, which will be `"mouse_scroll"`. - **dir** (number) - Indicates the scroll direction (-1 for up, 1 for down). - **x** (number) - The horizontal position of the mouse cursor. - **y** (number) - The vertical position of the mouse cursor. #### Response Example ``` The mouse was scrolled in direction -1 at 150, 300 The mouse was scrolled in direction 1 at 160, 310 ``` ``` -------------------------------- ### Capture and Display mouse_up Event in Lua Source: https://tweaked.cc/event/mouse_up This Lua code snippet continuously listens for the 'mouse_up' event. When the event occurs, it captures the mouse button and its coordinates, then prints this information to the console. This example demonstrates basic event handling and output in Lua. ```Lua while true do local event, button, x, y = os.pullEvent("mouse_up") print(("The mouse button %s was released at %d, %d"):format(button, x, y)) end ``` -------------------------------- ### Handle WebSocket Connection Failure with websocket_failure Source: https://tweaked.cc/event/websocket_failure This example demonstrates how to use the `websocket_failure` event to catch and report errors when a WebSocket connection cannot be established. It specifically listens for failures related to a given URL and prints the error message. This is useful for debugging network issues or when dealing with unreliable WebSocket servers. ```lua local myURL = "wss://example.tweaked.cc/not-a-websocket" http.websocketAsync(myURL) local event, url, err repeat event, url, err = os.pullEvent("websocket_failure") until url == myURL print("The URL " .. url .. " could not be reached: " .. err) ``` -------------------------------- ### Handle WebSocket Closed Event in Lua Source: https://tweaked.cc/event/websocket_closed This Lua snippet demonstrates how to listen for and handle the `websocket_closed` event. It connects to a WebSocket, waits for the connection to close, and then prints a message indicating the closed URL. This example relies on the `http` and `os` libraries. ```lua local myURL = "wss://example.tweaked.cc/echo" local ws = http.websocket(myURL) local event, url repeat event, url = os.pullEvent("websocket_closed") until url == myURL print("The WebSocket at " .. url .. " was closed.") ``` -------------------------------- ### Lua: Set and Wait for Alarm Event Source: https://tweaked.cc/event/alarm This snippet demonstrates how to use `os.setAlarm` to start a timer and then use `os.pullEvent` to wait for the 'alarm' event to complete. It ensures the correct alarm ID is received before printing a confirmation message. This function is essential for time-based operations in the environment. ```lua local alarm_id = os.setAlarm(os.time() + 0.05) local event, id repeat event, id = os.pullEvent("alarm") until id == alarm_id print("Alarm with ID " .. id .. " was fired") ``` -------------------------------- ### Default Terminate Event Handling with os.pullEvent Source: https://tweaked.cc/event/terminate This code snippet illustrates the default behavior of the 'terminate' event. When os.pullEvent() is used without specific filters, it handles the 'terminate' event internally, typically leading to program exit. This example shows a loop that continuously calls os.pullEvent(), effectively exiting when Ctrl-T is pressed. ```lua while true do os.pullEvent() end ``` -------------------------------- ### Key Press Event Documentation Source: https://tweaked.cc/event/key Details the 'key' event, its return values, and usage guidance. ```APIDOC ## key Event ### Description This event is fired when any key is pressed while the terminal is focused. It returns a numerical key code. ### Method N/A (Event) ### Endpoint N/A (Event) ### Parameters N/A ### Request Example N/A ### Response #### Event Data - **event_name** (string) - The name of the event, which is 'key'. - **key_code** (number) - The numerical key code of the key pressed. Use `keys` API constants for better compatibility. - **is_held** (boolean) - True if the key is being held down, false if it was just pressed. #### Response Example ```lua local event, key_code, is_held = os.pullEvent("key") -- Example: Prints the name of the key and whether it's being held print(keys.getName(key_code) .. " held=" .. tostring(is_held)) ``` ### Notes - If the pressed key represents a printable character, it will be immediately followed by a `char` event. For text input, prefer using the `char` event. - It is recommended to use constants from the `keys` API instead of hardcoding numerical key codes due to potential variations between versions. ``` -------------------------------- ### Monitor Touch Event Handling (Lua) Source: https://tweaked.cc/event/monitor_touch This snippet demonstrates how to handle the monitor_touch event. It continuously listens for the event and prints a message indicating which monitor was touched and at what coordinates. No external dependencies are required beyond the standard os library. ```lua while true do local event, side, x, y = os.pullEvent("monitor_touch") print("The monitor on side " .. side .. " was touched at (" .. x .. ", " .. y .. ")") end ``` -------------------------------- ### Listen for disk Insertion Event (Lua) Source: https://tweaked.cc/event/disk This code snippet demonstrates how to continuously listen for the 'disk' event using `os.pullEvent`. When a disk is inserted, it prints a message indicating the side of the disk drive. This pattern is common for handling asynchronous events in the environment. ```lua while true do local event, side = os.pullEvent("disk") print("Inserted a disk on side " .. side) end ``` -------------------------------- ### List Transferred Files and Sizes (Lua) Source: https://tweaked.cc/event/file_transfer This snippet demonstrates how to wait for the `file_transfer` event, retrieve the list of transferred files, and print the name and size of each file. It utilizes `os.pullEvent`, `files.getFiles`, `file.seek`, and `file.getName`. ```lua local _, files = os.pullEvent("file_transfer") for _, file in ipairs(files.getFiles()) do -- Seek to the end of the file to get its size, then go back to the beginning. local size = file.seek("end") file.seek("set", 0) print(file.getName() .. " " .. size) end ``` -------------------------------- ### File Transfer Event Handling Source: https://tweaked.cc/event/file_transfer This section details the 'file_transfer' event, which is triggered when a user drags and drops files onto the computer. It provides information on the event arguments and how to process the transferred files. ```APIDOC ## File Transfer Event ### Description The `file_transfer` event is queued when a user drags-and-drops a file on an open computer. This event contains a single argument of type `TransferredFiles`, which can be used to get the files to be transferred. Each file returned is a binary file handle with an additional getName method. ### Return Values 1. `string`: The event name (`"file_transfer"`) 2. `TransferredFiles`: An object containing the list of transferred files. ### Types #### TransferredFiles A list of files that have been transferred to this computer. ##### Methods * **`getFiles()`** * **Description**: Returns all the files that are being transferred to this computer. * **Returns**: `{ TransferredFile... }` - The list of files. #### TransferredFile A binary file handle that has been transferred to this computer. This inherits all methods of binary file handles, meaning you can use the standard read functions to access the contents of the file. ##### Methods * **`getName()`** * **Description**: Get the name of this file being transferred. * **Returns**: `string` - The file's name. ### Example Usage #### Printing File Names and Sizes Waits for a user to drop files on top of the computer, then prints the list of files and the size of each file. ```lua local _, files = os.pullEvent("file_transfer") for _, file in ipairs(files.getFiles()) do -- Seek to the end of the file to get its size, then go back to the beginning. local size = file.seek("end") file.seek("set", 0) print(file.getName() .. " " .. size) end ``` #### Saving Transferred Files Save each transferred file to the computer's storage. ```lua local _, files = os.pullEvent("file_transfer") for _, file in ipairs(files.getFiles()) do local handle = fs.open(file.getName(), "wb") handle.write(file.readAll()) handle.close() file.close() end ``` ### Changes * **New in version 1.101.0** ### See Also * **`fs.ReadHandle`** ``` -------------------------------- ### Handle WebSocket Message with Lua Source: https://tweaked.cc/event/websocket_message This Lua code demonstrates how to manually receive and process messages from a WebSocket connection using the websocket_message event. It establishes a connection, sends a message, and then enters a loop to listen for incoming messages, printing the content when a message from the specified URL is received. ```lua local myURL = "wss://example.tweaked.cc/echo" local ws = http.websocket(myURL) ws.send("Hello!") local event, url, message repeat event, url, message = os.pullEvent("websocket_message") until url == myURL print("Received message from " .. url .. " with contents " .. message) ws.close() ``` -------------------------------- ### Monitor Resize Event Listener in Lua Source: https://tweaked.cc/event/monitor_resize Listens for the 'monitor_resize' event and prints a message indicating which monitor was resized. This script runs indefinitely, continuously monitoring for size changes on connected or networked monitors. ```lua while true do local event, side = os.pullEvent("monitor_resize") print("The monitor on side " .. side .. " was resized.") end ``` -------------------------------- ### Handling key_up Events with Lua Source: https://tweaked.cc/event/key_up This snippet demonstrates how to handle the `key_up` event in Lua. It continuously listens for key release events, retrieves the key's name using the `keys.getName` function, and prints the name of the released key. It utilizes `os.pullEvent` to capture the event and the associated key code. ```lua while true do local event, key = os.pullEvent("key_up") local name = keys.getName(key) or "unknown key" print(name .. " was released.") end ``` -------------------------------- ### Capture and Display Key Presses with 'key' Event (Lua) Source: https://tweaked.cc/event/key This snippet demonstrates how to use the 'key' event to capture key presses in the terminal. It prints the name of the key pressed and whether it is being held. It relies on the 'os.pullEvent' function and the 'keys' API for key identification. ```lua while true do local event, key, is_held = os.pullEvent("key") print( העולם.format(keys.getName(key), is_held)) end ``` -------------------------------- ### HTTP Check Event Details Source: https://tweaked.cc/event/http_check Details about the http_check event, including its return values and related functions. ```APIDOC ## http_check Event ### Description The `http_check` event is fired when a URL check finishes. This event is normally handled inside `http.checkURL`, but it can still be seen when using `http.checkURLAsync`. ### Return Values 1. `string`: The event name. (Always "http_check") 2. `string`: The URL that was requested to be checked. 3. `boolean`: Indicates whether the URL check succeeded (`true`) or failed (`false`). 4. `string`|`nil`: If the check failed, this value contains a string explaining the reason for the failure. Otherwise, it is `nil`. ### See also * **`http.checkURLAsync`**: To check a URL asynchronously. ``` -------------------------------- ### Handle Terminal Resize Events (Lua) Source: https://tweaked.cc/event/term_resize This snippet demonstrates how to continuously listen for and react to the 'term_resize' event. It retrieves the new terminal dimensions and prints a message. Applications should use this to redraw their UI when the terminal resizes. ```lua while true do os.pullEvent("term_resize") local w, h = term.getSize() print("The term was resized to (" .. w .. ", " .. h .. ")") end ``` -------------------------------- ### Handle WebSocket Success with Lua Source: https://tweaked.cc/event/websocket_success This snippet demonstrates how to handle the websocket_success event in Lua, which fires when a WebSocket connection is established. It utilizes http.websocketAsync to initiate the connection and os.pullEvent to capture the success event, then sends and receives data over the established WebSocket connection. ```Lua local myURL = "wss://example.tweaked.cc/echo" http.websocketAsync(myURL) local event, url, handle repeat event, url, handle = os.pullEvent("websocket_success") until url == myURL print("Connected to " .. url) handle.send("Hello!") print(handle.receive()) handle.close() ``` -------------------------------- ### Listen for modem_message Events in Lua Source: https://tweaked.cc/event/modem_message This snippet demonstrates how to listen for and process incoming messages using the modem_message event in Lua. It requires a modem peripheral to be attached and opens channel 0 for communication. The code continuously pulls events, extracts message details, and prints them to the console. It handles different message types and distance information. ```lua local modem = peripheral.find("modem") or error("No modem attached", 0) modem.open(0) while true do local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message") print(("Message received on side %s on channel %d (reply to %d) from %f blocks away with message %s"):format( side, channel, replyChannel, distance, tostring(message) )) end ``` -------------------------------- ### Handle 'peripheral' Event in Lua Source: https://tweaked.cc/event/peripheral This snippet demonstrates how to listen for the 'peripheral' event and print a message indicating which side a new peripheral has been attached to. It uses a loop to continuously pull events. ```lua while true do local event, side = os.pullEvent("peripheral") print("A peripheral was attached on side " .. side) end ``` -------------------------------- ### Handle Asynchronous Task Completion with task_complete Event (Lua) Source: https://tweaked.cc/event/task_complete This snippet demonstrates how to use the 'task_complete' event to wait for and process the results of an asynchronous command executed via commands.execAsync. It polls for the specific task completion event and then checks for success or failure, printing the results accordingly. ```lua local taskID = commands.execAsync("say Hello") local event repeat event = {os.pullEvent("task_complete")} until event[2] == taskID if event[3] == true then print("Task " .. event[2] .. " succeeded:", table.unpack(event, 4)) else print("Task " .. event[2] .. " failed: " .. event[4]) end ``` -------------------------------- ### Handle mouse_click Event in Lua Source: https://tweaked.cc/event/mouse_click This snippet demonstrates how to capture and process the mouse_click event in Lua. It continuously pulls for the 'mouse_click' event and prints the details of the click, including the mouse button and coordinates. This requires an advanced computer or turtle. ```lua while true do local event, button, x, y = os.pullEvent("mouse_click") print(("The mouse button %s was pressed at %d, %d"):format(button, x, y)) end ``` -------------------------------- ### Capture Typed Characters with 'char' Event Source: https://tweaked.cc/event/char This snippet demonstrates how to continuously capture and print characters typed by the user using the 'char' event. It utilizes a loop to poll for the 'char' event and prints the associated character. ```lua while true do local event, character = os.pullEvent("char") print(character .. " was pressed.") end ``` -------------------------------- ### Handle Incoming Rednet Messages with rednet_message Source: https://tweaked.cc/event/rednet_message This Lua code snippet demonstrates how to continuously listen for and process incoming Rednet messages. It utilizes os.pullEvent to capture the 'rednet_message' event and then prints the sender, message, and protocol (if available). This is a fundamental pattern for Rednet communication. ```lua while true do local event, sender, message, protocol = os.pullEvent("rednet_message") if protocol ~= nil then print("Received message from " .. sender .. " with protocol " .. protocol .. " and message " .. tostring(message)) else print("Received message from " .. sender .. " with message " .. tostring(message)) end end ``` -------------------------------- ### Listen for disk_eject Event in Lua Source: https://tweaked.cc/event/disk_eject This snippet demonstrates how to continuously listen for the 'disk_eject' event in Lua. When a disk is removed, it prints the side of the disk drive where the removal occurred. This functionality is useful for reacting to disk removal actions in a system. ```lua while true do local event, side = os.pullEvent("disk_eject") print("Removed a disk on side " .. side) end ``` -------------------------------- ### Listen for Redstone Input Changes with Lua Source: https://tweaked.cc/event/redstone This snippet demonstrates how to continuously monitor for changes in redstone inputs. It uses `os.pullEvent` to wait for the 'redstone' event and prints a message when detected. This is useful for creating interactive redstone contraptions. ```lua while true do os.pullEvent("redstone") print("A redstone input has changed!") end ``` -------------------------------- ### Monitor Turtle Inventory Changes (Lua) Source: https://tweaked.cc/event/turtle_inventory This snippet demonstrates how to use the turtle_inventory event to detect when a turtle's inventory has changed. It continuously monitors for the event and prints a message to the console upon detection. No external dependencies are required beyond the core API. ```lua while true do os.pullEvent("turtle_inventory") print("The inventory was changed.") end ``` -------------------------------- ### Handle HTTP Success Event with Lua Source: https://tweaked.cc/event/http_success This snippet demonstrates how to use os.pullEvent to capture the 'http_success' event when making an HTTP request. It specifically waits for a response matching a given URL and then prints the content of the response. This requires the http module and os.pullEvent. ```lua local myURL = "https://tweaked.cc/" http.request(myURL) local event, url, handle repeat event, url, handle = os.pullEvent("http_success") until url == myURL print("Contents of " .. url .. ":") print(handle.readAll()) handle.close() ``` -------------------------------- ### Handle HTTP Failure Event in Lua Source: https://tweaked.cc/event/http_failure This Lua code snippet demonstrates how to capture and process the 'http_failure' event. It shows how to retrieve the event name, requested URL, error message, and response handle. This is useful for debugging network issues or gracefully handling inaccessible web resources. ```lua local myURL = "https://does.not.exist.tweaked.cc" http.request(myURL) local event, url, err repeat event, url, err = os.pullEvent("http_failure") until url == myURL print("The URL " .. url .. " could not be reached: " .. err) ``` ```lua local myURL = "https://tweaked.cc/this/does/not/exist" http.request(myURL) local event, url, err, handle repeat event, url, err, handle = os.pullEvent("http_failure") until url == myURL print("The URL " .. url .. " could not be reached: " .. err) print(handle.getResponseCode()) handle.close() ``` -------------------------------- ### key_up Event Source: https://tweaked.cc/event/key_up Fired whenever a key is released or the terminal is closed while a key was being pressed. Returns a numerical key code. ```APIDOC ## key_up Event ### Description Fired whenever a key is released (or the terminal is closed while a key was being pressed). This event returns a numerical "key code" (for instance, `F1` is 290). This value may vary between versions and so it is recommended to use the constants in the `keys` API rather than hard coding numeric values. ### Method Event ### Endpoint key_up ### Return Values - **event** (string) - The event name, which will be "key_up". - **key** (number) - The numerical key value of the key pressed. ### Request Example ```lua -- Example of how to listen for the key_up event while true do local event, key = os.pullEvent("key_up") if event == "key_up" then local name = keys.getName(key) or "unknown key" print(name .. " was released.") end end ``` ### Response #### Success Response - **event** (string) - The name of the event that occurred. - **key** (number) - The numerical code of the key that was released. #### Response Example ```json { "event": "key_up", "key": 28 } ``` ### See Also - **`keys`**: For a lookup table of the given keys. ``` -------------------------------- ### task_complete Event Source: https://tweaked.cc/event/task_complete The task_complete event is fired when an asynchronous task completes. It provides information about the task's success and any returned data. ```APIDOC ## task_complete Event ### Description The `task_complete` event is fired when an asynchronous task completes. This is usually handled inside the function call that queued the task; however, functions such as `commands.execAsync` return immediately so the user can wait for completion. ### Return Values 1. `string`: The event name. 2. `number`: The ID of the task that completed. 3. `boolean`: Whether the command succeeded. 4. `string`: If the command failed, an error message explaining the failure. (This is not present if the command succeeded.) 5. `...`: Any parameters returned from the command. ### Example Prints the results of an asynchronous command: ```lua local taskID = commands.execAsync("say Hello") local event repeat event = {os.pullEvent("task_complete")} until event[2] == taskID if event[3] == true then print("Task " .. event[2] .. " succeeded:", table.unpack(event, 4)) else print("Task " .. event[2] .. " failed: " .. event[4]) end ``` ### See Also * **`commands.execAsync`**: To run a command which fires a task_complete event. ``` -------------------------------- ### Handle computer_command Event in Lua Source: https://tweaked.cc/event/computer_command This snippet demonstrates how to listen for and process the 'computer_command' event. It continuously pulls events and prints the arguments received from the command. This is useful for creating interactive command-driven systems within ComputerCraft. ```lua while true do local event = {os.pullEvent("computer_command")} print("Received message:", table.unpack(event, 2)) end ``` -------------------------------- ### Print mouse drag details in Lua Source: https://tweaked.cc/event/mouse_drag This Lua code snippet continuously listens for the 'mouse_drag' event and prints the pressed mouse button along with the X and Y coordinates whenever the event occurs. It requires no external libraries beyond the standard 'os' module. ```lua while true do local event, button, x, y = os.pullEvent("mouse_drag") print(("The mouse button %s was dragged at %d, %d"):format(button, x, y)) end ``` -------------------------------- ### http_success Event Documentation Source: https://tweaked.cc/event/http_success This section details the http_success event, which occurs when an HTTP request completes successfully. It is typically handled internally by http.get and http.post, but can also be observed when using http.request. ```APIDOC ## http_success Event ### Description The `http_success` event is fired when an HTTP request returns successfully. This event is normally handled inside `http.get` and `http.post`, but it can still be seen when using `http.request`. ### Event Name `"http_success"` ### Return Values 1. `string`: The event name. (Always `"http_success"`) 2. `string`: The URL of the site requested. 3. `http.Response`: The successful HTTP response object. ### Example Prints the content of a website (this may fail if the request fails): ```lua local myURL = "https://tweaked.cc/" http.request(myURL) local event, url, handle repeat event, url, handle = os.pullEvent("http_success") until url == myURL print("Contents of " .. url .. ":") print(handle.readAll()) handle.close() ``` ### See Also * **`http.request`**: To make an HTTP request. ``` -------------------------------- ### Mouse Up Event Source: https://tweaked.cc/event/mouse_up This event is fired when a mouse button is released or a held mouse leaves the computer's terminal. It returns the event name, the mouse button that was released, and the X and Y coordinates of the mouse. ```APIDOC ## mouse_up Event ### Description This event is fired when a mouse button is released or a held mouse leaves the computer's terminal. ### Method Event Trigger ### Endpoint N/A (Event-based) ### Parameters None ### Request Body None ### Request Example None ### Response #### Return Values - **event** (string): The event name, which is 'mouse_up'. - **button** (number): The mouse button that was released (e.g., 1 for left, 2 for middle, 3 for right). - **x** (number): The X-coordinate of the mouse cursor. - **y** (number): The Y-coordinate of the mouse cursor. #### Response Example ```lua local event, button, x, y = os.pullEvent("mouse_up") print(("The mouse button %s was released at %d, %d"):format(button, x, y)) ``` ``` -------------------------------- ### Handle Terminate Event with os.pullEventRaw Source: https://tweaked.cc/event/terminate This snippet demonstrates how to use os.pullEventRaw to capture the 'terminate' event, which is triggered when Ctrl-T is held down. It prints a message when the event occurs. It's important to explicitly check if the received event is indeed 'terminate' when using os.pullEventRaw with filters. ```lua while true do local event = os.pullEventRaw("terminate") if event == "terminate" then print("Terminate requested!") end end ``` -------------------------------- ### Capture and Print Pasted Text - Lua Source: https://tweaked.cc/event/paste This snippet demonstrates how to capture the 'paste' event and print the text that was pasted. It uses a loop to continuously listen for paste events. No external dependencies are required. ```lua while true do local event, text = os.pullEvent("paste") print('"' .. text .. '" was pasted') end ``` -------------------------------- ### Handle peripheral_detach Event with Lua Source: https://tweaked.cc/event/peripheral_detach This snippet demonstrates how to continuously listen for the peripheral_detach event using Lua's os.pullEvent function. It prints a message indicating which side a peripheral was detached from. This is useful for reacting to hardware changes in real-time. ```lua while true do local event, side = os.pullEvent("peripheral_detach") print("A peripheral was detached on side " .. side) end ``` -------------------------------- ### Handle mouse_scroll Event in Lua Source: https://tweaked.cc/event/mouse_scroll This snippet demonstrates how to capture and process the 'mouse_scroll' event in Lua. It continuously listens for scroll events, printing the scroll direction and the mouse's X and Y coordinates at the time of the scroll. This is useful for implementing scroll-based interactions within a terminal application. ```lua while true do local event, dir, x, y = os.pullEvent("mouse_scroll") print(("The mouse was scrolled in direction %s at %d, %d"):format(dir, x, y)) end ``` -------------------------------- ### rednet_message Event Source: https://tweaked.cc/event/rednet_message The rednet_message event is fired when a message is sent over Rednet. It is typically handled by `rednet.receive` or can be processed manually. This event is generated within CraftOS in response to modem_message events. ```APIDOC ## rednet_message Event ### Description The `rednet_message` event is fired when a message is sent over Rednet. This event is usually handled by `rednet.receive`, but it can also be pulled manually. `rednet_message` events are sent by `rednet.run` in the top-level coroutine in response to `modem_message` events. A `rednet_message` event is always preceded by a `modem_message` event. They are generated inside CraftOS rather than being sent by the ComputerCraft machine. ### Return Values 1. `string`: The event name. 2. `number`: The ID of the sending computer. 3. `any`: The message sent. 4. `string`|`nil`: The protocol of the message, if provided. ### Example Prints a message when one is sent: ```lua while true do local event, sender, message, protocol = os.pullEvent("rednet_message") if protocol ~= nil then print("Received message from " .. sender .. " with protocol " .. protocol .. " and message " .. tostring(message)) else print("Received message from " .. sender .. " with message " .. tostring(message)) end end ``` ### See also * **`modem_message`**: For raw modem messages sent outside of Rednet. * **`rednet.receive`**: To wait for a Rednet message with an optional timeout and protocol filter. ``` -------------------------------- ### disk_eject Event Source: https://tweaked.cc/event/disk_eject The disk_eject event is triggered when a disk is removed from an adjacent or networked disk drive. It returns the event name and the side of the disk drive from which the disk was removed. ```APIDOC ## disk_eject Event ### Description The `disk_eject` event is fired when a disk is removed from an adjacent or networked disk drive. ### Parameters #### Return Values - **event** (string) - The name of the event, which is "disk_eject". - **side** (string) - The side of the disk drive that had a disk removed. ### Request Example ```lua while true do local event, side = os.pullEvent("disk_eject") print("Removed a disk on side " .. side) end ``` ### See Also * **`disk`**: For the event sent when a disk is inserted. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.