Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
guides:v1_v2_cheat_sheet [2023-03-21 16:12] – created iseahound | guides:v1_v2_cheat_sheet [2025-01-13 19:37] (current) – [v1→2 Conversion Cheat Sheet] Add auto-converter widget to page geek | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | v1 → c2 cheat sheet is a great idea. The purpose wouldn' | + | ====== |
- | 1. Removing legacy assignment with `=` | + | ===== Converter ===== |
- | 2. Removing `%` force expression | + | Attempt to convert AHKv1 syntax |
- | 3. Functions are commands, and commands are functions. The fiest comma is removed. | + | < |
+ | ; Requires AutoHotkey v1.1.26+, and the keyboard hook must be installed. | ||
+ | # | ||
+ | SendSuppressedKeyUp(key) { | ||
+ | DllCall(" | ||
+ | , " | ||
+ | , " | ||
+ | , " | ||
+ | , " | ||
+ | } | ||
- | 4. Building from 3, you can do | + | ; Disable Alt+key shortcuts for the IME. |
+ | ~LAlt:: | ||
- | if (3 < 4) | + | ; Test hotkey: |
- | FAIL | + | !CapsLock:: |
- | FAIL() | + | ; Remap CapsLock to LCtrl in a way compatible with IME. |
- | | + | *CapsLock:: |
- | } | + | Send {Blind}{LCtrl DownR} |
+ | | ||
+ | | ||
+ | *CapsLock up:: | ||
+ | Send {Blind}{LCtrl Up} | ||
+ | return | ||
+ | </ | ||
- | 5. Fat arrow syntax allows easy definition of functions | + | ===== General ===== |
- | FAIL() => MsgBox(" | + | - There is no more Auto Execute // |
+ | - You no longer need the boilerplate at the top of each script. ''# | ||
+ | - Legacy assignments ('' | ||
+ | - Commands do not support a first comma anymore.\\ Change ❌'' | ||
+ | - All commands have been turned into functions. Command syntax is just a function call without parentheses (commands can be //any// function, even user defined ones). Consequently, | ||
+ | - Memory is reserved using '' | ||
+ | - Ampersand (''&'' | ||
+ | - Guis are fully object-oriented, | ||
+ | - Multi-value expressions return the last value, not the first value.\\ Change ❌'' | ||
+ | - Single-quoted strings can contain text with double quotes. Doubling up quote marks no longer escapes quotes.\\ Change ❌'' | ||
+ | - Hotkeys are now functions. They do not need a '' | ||
+ | global varWritable | ||
+ | | ||
+ | MsgBox "Bye " | ||
+ | }</ | ||
+ | - Variables start as '' | ||
- | 6. v2 has proper support for memory buffers using `Buffer(size)` | + | **New Stuff** |
- | 7. Byref has been replaced by `&`. | + | - For loops can iterate values directly. In v1, the variable i will return 1, 2, 3... this is not a problem in v2 and will return a, b c.<code AutoHotkey> |
+ | MsgBox v</ | ||
+ | - '' | ||
- | 8. ComCall can iterate through a virtual function table without 3 DllCalls | ||
- | 9. The auto execute section has been removed. Instead, any global code is run, including `static __new()`. | + | ===== Functions ===== |
- | 10. Gui has better syntax | + | - Functions can read Global Variables by default, but not write to them. |
+ | - ByRef is now done by ampersand, | ||
- | 11. Classes now have a `prototype` object. | + | **New Stuff** |
- | 12. You can't mix `static` and `instance` methods anymore. Static methods can only be called `Name_of_class.method()` and instance methods | + | - First class functions are supported. So you can use '' |
+ | execute(func) { | ||
+ | func(" | ||
+ | } | ||
+ | execute(MsgBox)</ | ||
+ | - Fat arrow syntax allows easy definition of functions | ||
+ | ; This code: | ||
+ | ;FAIL() { | ||
+ | ; MsgBox " | ||
+ | ;} | ||
+ | ; Can become: | ||
+ | FAIL() => MsgBox(" | ||
- | 13. You can't use `obj.1` or `obj.2` to access data. You have to use `obj[1]` and `obj[2]`. | + | ; This code: |
- | + | ; | |
- | 14. This is because AHK v2 has better support for data structures. It incluses array() and map(). | + | ; x *= 5 |
- | + | ; MsgBox "Times 5: " x | |
- | 15. for loops can iterate values directly. In v1, the variable i will return | + | ; |
- | + | ;} | |
- | + | ;FuncObj := Func("SomeGlobalFunc" | |
- | for i in ["a", "b", | + | ; Can become (no longer needing |
- | | + | FuncObj := (x) => (x *= 5, MsgBox("Times 5: " |
- | + | </ | |
- | + | - Functions can be defined inside functions, forming // | |
- | 16. v2 supports first class functions. So you can use `MsgBox` as a function reference. For example, | + | closureFactory(valueToBeEnclosed) { |
- | + | | |
- | | + | |
- | | + | |
} | } | ||
+ | return myClosure | ||
+ | } | ||
+ | myFuncObject := closureFactory(24) | ||
+ | MsgBox myFuncObject() | ||
+ | myFuncObject(25) | ||
+ | MsgBox myFuncObject() | ||
+ | </ | ||
- | execute(MsgBox) | ||
- | In v1, you had to pass `execute(Func(" | + | ===== Objects ===== |
- | 17. In multivalue return statements, v1 returns | + | - Objects now have specific sub-types, '' |
+ | - Objects now have two key value stores, one for // | ||
+ | MsgBox data.Count ; Access Property " | ||
+ | MsgBox data[" | ||
- | return " | ||
- | v2 would return " | + | ===== Classes ===== |
- | 18. You can use single quotes like `'this'`. | + | - You do not use '' |
- | + | - Classes create both a //Global Object// and a // | |
- | 19. Hotkeys | + | - You can't mix " |
- | + | | |
- | | + | Property |
- | MsgBox "hi" | + | static Method() |
- | MsgBox "bye" | + | MsgBox "Static Method: |
} | } | ||
- | + | Method() { | |
- | You don't need a return anymore. | + | |
- | + | } | |
- | 20. You no longer need the boilerplate on top of each v1 script. | + | } |
- | + | Test.Method() | |
- | # | + | Test().Method() |
- | | + | |
- | | + | |
- | SetWorkingDir %A_ScriptDir% | + | |
- | + | ||
- | the above is now the default. Also `SetBatchLines -1` is default as well, so your script will run much faster. | + |