![]() You should try this function to see that it works. Looking at the documentation for newMouseEvent(), we see that the parameter is optional, so for this particular function our use is OK. In this example, the function can be called as leftDoubleClick(), without any parameters, which means the modifiers parameter might have a nil value. Since we are generating system events directly, we also need to take care of generating a “mouse up” event at the end.įunction parameters are always optional, and those not passed will default to nil, so you need to do proper validation. This allows us to chain the calls as shown: setProperty() is called on the hs.eventtap object returned by newMouseEvent to set its type to a double click, and post() is called on the result to issue the event. We create a new mouse event of type leftMouseDown in the obtained coordinates and with the given modifiers.īy convention, most Hammerspoon API methods return the same object on which they operate. We first get the current position of the mouse pointer using hs.mouse.getAbsolutePosition. This function simulates a double click on the current pointer position: In this example we can also see some examples of the Hammerspoon library in action, in particular two extremely powerful libraries: hs.mouse for interacting with the mouse pointer, and hs.eventtap, which allows you to both intercept and generate arbitrary system events, including key pressed and mouse clicks. leftMouseUp, pos, modifiers ): post () end mouseEventClickState, 2 ) : post () - hs.eventtap. leftMouseDown, pos, modifiers ) - : setProperty ( hs.eventtap. This is simply a syntactic shortcut, i.e. the following two are equivalent:įunction leftDoubleClick ( modifiers ) local pos = hs.mouse. The latter is Lua’s object-method-call notation, and its effect is to pass the object on which the method is being called as an implicit first argument called self. You will notice that sometimes, functions contained within a module are called with a dot, and others with a colon. In practice, I’ve found myself using both notations somewhat exchangeably - feel free to use whichever you find most comfortable. Since the file variable is being passed as the first argument, you could rewrite this statement as file:match('/'). In string.match(file, '/'), we see the function notation used to call string.match(). This statement is equivalent to string.sub(file, -4). This invokes the string.sub() function, automatically passing the file variable as its first argument. In file:sub(-4), the colon indicates the object-oriented notation (see “Lua dot-vs-colon method access” below). Our first glimpse into the Lua string library, and the two ways in which it can be used: In this case we are not interested in the key in each iteration, just the value, so we assign the key to _, never to be used again. The _ variable, while not special per se, is used by convention in Lua for “throwaway values”. pairs() treats the files variable as an associative array, and returns in each iteration a key/value pair of its contents. ![]() ![]() We will learn more about Lua tables below, but they can be used to represent both regular and associative arrays. The pairs() function, which loops over a table. Going back to our example, we can also learn the following: This form loops the variable from the first to the last value, incrementing it by the given increment (defaults to 1) at each iteration. Lua includes all the common flow-control structures you might expect. Instead of detailing every structure, I will focus on the aspects that are most different or that are most likely to trip you up as you learn it. I assume you are a programmer who knows some other C-like language–if you already know C, Java, Ruby, Python, Perl, Javascript or some similar language, picking up Lua should be pretty easy. ![]() The purpose of this section is to give you a quick overview of the Lua features and peculiarities you may find most useful for developing Hammerspoon policies. It is easy to learn and use while having pretty powerful features, and is frequently used in games, but also in many other applications including, of course, Hammerspoon. Lua is a scripting language created in 1993, and focused from the beginning in being an embedded language for extending other applications. ![]() Along the way you will discover that Lua is a surprisingly powerful language. In this 2-part article I will show you the basics of Lua so you can read and write Hammerspoon configuration. Hammerspoon’s configuration files are written in Lua, so a basic knowledge of the language is very useful to be an effective user of Hammerspoon. Just Enough Lua to Be Productive in Hammerspoon, Part 1 ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |