mirror of
				https://github.com/hajimehoshi/ebiten.git
				synced 2025-10-31 19:52:47 +08:00 
			
		
		
		
	| @@ -27,6 +27,7 @@ import ( | |||||||
| 	"github.com/hajimehoshi/ebiten/v2/ebitenutil" | 	"github.com/hajimehoshi/ebiten/v2/ebitenutil" | ||||||
| 	"github.com/hajimehoshi/ebiten/v2/examples/keyboard/keyboard" | 	"github.com/hajimehoshi/ebiten/v2/examples/keyboard/keyboard" | ||||||
| 	rkeyboard "github.com/hajimehoshi/ebiten/v2/examples/resources/images/keyboard" | 	rkeyboard "github.com/hajimehoshi/ebiten/v2/examples/resources/images/keyboard" | ||||||
|  | 	"github.com/hajimehoshi/ebiten/v2/inpututil" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| const ( | const ( | ||||||
| @@ -46,16 +47,9 @@ func init() { | |||||||
| } | } | ||||||
|  |  | ||||||
| type Game struct { | type Game struct { | ||||||
| 	pressed []ebiten.Key |  | ||||||
| } | } | ||||||
|  |  | ||||||
| func (g *Game) Update() error { | func (g *Game) Update() error { | ||||||
| 	g.pressed = nil |  | ||||||
| 	for k := ebiten.Key(0); k <= ebiten.KeyMax; k++ { |  | ||||||
| 		if ebiten.IsKeyPressed(k) { |  | ||||||
| 			g.pressed = append(g.pressed, k) |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -73,7 +67,8 @@ func (g *Game) Draw(screen *ebiten.Image) { | |||||||
|  |  | ||||||
| 	// Draw the highlighted keys. | 	// Draw the highlighted keys. | ||||||
| 	op = &ebiten.DrawImageOptions{} | 	op = &ebiten.DrawImageOptions{} | ||||||
| 	for _, p := range g.pressed { | 	keys := inpututil.PressedKeys() | ||||||
|  | 	for _, p := range keys { | ||||||
| 		op.GeoM.Reset() | 		op.GeoM.Reset() | ||||||
| 		r, ok := keyboard.KeyRect(p) | 		r, ok := keyboard.KeyRect(p) | ||||||
| 		if !ok { | 		if !ok { | ||||||
| @@ -85,7 +80,7 @@ func (g *Game) Draw(screen *ebiten.Image) { | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	keyStrs := []string{} | 	keyStrs := []string{} | ||||||
| 	for _, p := range g.pressed { | 	for _, p := range keys { | ||||||
| 		keyStrs = append(keyStrs, p.String()) | 		keyStrs = append(keyStrs, p.String()) | ||||||
| 	} | 	} | ||||||
| 	ebitenutil.DebugPrint(screen, strings.Join(keyStrs, ", ")) | 	ebitenutil.DebugPrint(screen, strings.Join(keyStrs, ", ")) | ||||||
|   | |||||||
| @@ -161,6 +161,23 @@ func (i *inputState) update() { | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // PressedKeys returns a set of currently pressed keyboard keys. | ||||||
|  | // | ||||||
|  | // PressedKeys is concurrent safe. | ||||||
|  | func PressedKeys() []ebiten.Key { | ||||||
|  | 	theInputState.m.RLock() | ||||||
|  | 	defer theInputState.m.RUnlock() | ||||||
|  |  | ||||||
|  | 	keys := make([]ebiten.Key, 0, len(theInputState.keyDurations)) | ||||||
|  | 	for i, d := range theInputState.keyDurations { | ||||||
|  | 		if d == 0 { | ||||||
|  | 			continue | ||||||
|  | 		} | ||||||
|  | 		keys = append(keys, ebiten.Key(i)) | ||||||
|  | 	} | ||||||
|  | 	return keys | ||||||
|  | } | ||||||
|  |  | ||||||
| // IsKeyJustPressed returns a boolean value indicating | // IsKeyJustPressed returns a boolean value indicating | ||||||
| // whether the given key is pressed just in the current frame. | // whether the given key is pressed just in the current frame. | ||||||
| // | // | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Hajime Hoshi
					Hajime Hoshi