Is there an example or method of getting a Windows system's idle time using Go?
I've been looking at the documentation at the Golang site but I think I'm missing how to access (and use) the API to get system information including the idle time.
相关问题
- the application was unable to start correctly 0xc0
- Handle button click in another application
- Golang mongodb aggregation
- How to flatten out a nested json structure in go
- how to install private repo using glide golang
相关文章
- Can I run a single test in a suite?
- How to check if a request was cancelled
- Is it possible to implement an interface with unex
- Why windows 64 still makes use of user32.dll etc?
- How to access value of first index of array in Go
- Can WM_NEXTDLGCTL be used with non-dialog windows?
- Embedded Interface
- Windows EventLog: How fast are operations with it?
Go's website is hardcoded to show the documentation for the standard library packages on Linux. You will need to get godoc and run it yourself:
then open
http://127.0.0.1:6060/
in your web browser.Of note is package
syscall
, which provides facilities for accessing functions in DLLs, including UTF-16 helpers and callback generation functions.Doing a quick recursive search of the Go tree says it doesn't have an API for
GetLastInputInfo()
in particular, so unless I'm missing something, you should be able to call that function from the DLL directly:Your case involves a structure. As far as I know, you can just recreate the structure flat (keeping fields in the same order), but you must convert any
int
fields in the original toint32
, otherwise things will break on 64-bit Windows. Consult the Windows Data Types page on MSDN for the appropriate type equivalents. In your case, this would beBecause this (like so many structs in the Windows API) has a
cbSize
field that requires you to initialize it with the size of the struct, we must do so too:Now we just need to pass a pointer to that
lastInputInfo
variable to the function:and just remember to import
syscall
andunsafe
.All args to
DLL/LazyDLL.Call()
areuintptr
, as is ther1
return. The_
return is never used on Windows (it has to do with the ABI used).Since I went over most of what you need to know to use the Windows API in Go that you can't gather from reading the
syscall
docs, I will also say (and this is irrelevant to the above question) that if a function has both ANSI and Unicode versions, you should use the Unicode versions (W
suffix) and the UTF-16 conversion functions in packagesyscall
for best results.I think that's all the info you (or anyone, for that matter) will need to use the Windows API in Go programs.
Regarding for answer from andlabs. This is ready for use example:
This is code print idle time every second. Try run and don't touch mouse/keyboard