-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
65 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//go:build js && wasm | ||
// +build js,wasm | ||
|
||
package keys | ||
|
||
import ( | ||
"syscall/js" | ||
|
||
"github.com/dimkauzh/webzen/src/backend/document" | ||
) | ||
|
||
var keyPressed = make(map[string]bool) | ||
|
||
func SetupEventListeners() { | ||
// Initialize event listeners once | ||
document.AddEventListener("keydown", js.FuncOf(func(this js.Value, p []js.Value) interface{} { | ||
event := p[0] | ||
key := event.Get("key").String() | ||
|
||
// Set the key's state to pressed. | ||
keyPressed[key] = true | ||
|
||
return nil | ||
})) | ||
|
||
document.AddEventListener("keyup", js.FuncOf(func(this js.Value, p []js.Value) interface{} { | ||
event := p[0] | ||
key := event.Get("key").String() | ||
|
||
// Set the key's state to released. | ||
keyPressed[key] = false | ||
|
||
return nil | ||
})) | ||
} | ||
|
||
func KeyPressed(key string) bool { | ||
return keyPressed[key] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,23 @@ | ||
package main | ||
|
||
import ( | ||
|
||
"github.com/dimkauzh/webzen/src/webzen" | ||
"github.com/dimkauzh/webzen/src/webzen/draw" | ||
"github.com/dimkauzh/webzen/src/webzen/keys" | ||
"github.com/dimkauzh/webzen/src/webzen/tools" | ||
) | ||
|
||
func main() { | ||
webzen.Init() | ||
draw.FillBackground([4]int{255, 255, 188, 255}) | ||
draw.DrawText("Hello under the world!", 21, 100, 100) | ||
draw.DrawRect(50, 500, 400, 400, [4]int{146, 255, 123, 255}) | ||
draw.DrawRect(200, 200, 100, 400, [4]int{146, 255, 123, 255}) | ||
for { | ||
draw.FillBackground([4]int{255, 255, 188, 255}) | ||
draw.DrawText("Hello under the world!", 21, 100, 100) | ||
draw.DrawRect(50, 500, 400, 400, [4]int{146, 255, 123, 255}) | ||
draw.DrawRect(200, 200, 100, 400, [4]int{146, 255, 123, 255}) | ||
if keys.KeyPressed("a") { | ||
tools.Print("A key pressed") | ||
} | ||
webzen.Update() | ||
} | ||
} |