fyne中的按键及快捷键
qingheluo2024-08-07清河洛295
fyne中的按键type KeyEvent表示一个键盘输入事件
type KeyEvent struct {
Name KeyName // 按键的名称
Physical HardwareKey
type HardwareKey struct {
ScanCode int // 表示键盘事件的硬件ID,不同平台值有差异
}
一般情况下应使用KeyName以实现跨平台兼容
}
type KeyName表示已按下的按键的名称type KeyName stringfyne定义了所有按键名称常量
con...
fyne中的按键
type KeyEvent
表示一个键盘输入事件
type KeyEvent struct { Name KeyName // 按键的名称 Physical HardwareKey type HardwareKey struct { ScanCode int // 表示键盘事件的硬件ID,不同平台值有差异 } 一般情况下应使用KeyName以实现跨平台兼容 }
type KeyName
表示已按下的按键的名称
type KeyName string
fyne定义了所有按键名称常量
const ( KeyEscape KeyName = "Escape" KeyReturn KeyName = "Return" // 回车 KeyEnter KeyName = "KP_Enter" // 小键盘回车 KeyTab KeyName = "Tab" KeyBackspace KeyName = "BackSpace" KeyInsert KeyName = "Insert" KeyDelete KeyName = "Delete" KeyRight KeyName = "Right" KeyLeft KeyName = "Left" KeyDown KeyName = "Down" KeyUp KeyName = "Up" KeyPageUp KeyName = "Prior" KeyPageDown KeyName = "Next" KeyHome KeyName = "Home" KeyEnd KeyName = "End" KeyF1-F12 KeyName = "F1-F12" Key0-9 KeyName = "0-9" KeyA-Z KeyName = "A-Z" KeySpace KeyName = "Space" KeyApostrophe KeyName = "'" KeyComma KeyName = "," KeyMinus KeyName = "-" KeyPeriod KeyName = "." KeySlash KeyName = "/" KeyBackslash KeyName = "\\" KeyLeftBracket KeyName = "[" KeyRightBracket KeyName = "]" KeySemicolon KeyName = ";" KeyEqual KeyName = "=" KeyAsterisk KeyName = "*" KeyPlus KeyName = "+" KeyBackTick KeyName = "`" KeyUnknown KeyName = "" )
在KeyName中,没有定义Ctrl、Alt、SHIFT、Win按键,在fyne中,这4个键被称为修饰键
type KeyModifier
表示与键一起按下的修饰键
type KeyModifier int
fyne中定义了4个修饰键的常量
const ( KeyModifierShift KeyModifier = 1 << iota KeyModifierControl KeyModifierAlt KeyModifierSuper // 表示Win键 )
fyne中的快捷键
fyne中的快捷键绑定是基于画布对象实现的,通过画布对象的AddShortcut()和RemoveShortcut来实现快捷方式的绑定和删除操作
AddShortcut(shortcut Shortcut, handler func(shortcut Shortcut)) RemoveShortcut(shortcut Shortcut)
并非所有的画布对象都可以绑定快捷键
fyne定义了一个接口,用于表示可以进行快捷键绑定的画布对象
type Shortcutable
type Shortcutable interface { TypedShortcut(Shortcut) } TypedShortcut方法用于执行注册的快捷方式绑定的函数
type Shortcut
表示一个按键快捷方式的接口实现的"基接口"
type Shortcut interface { ShortcutName() string }
KeyboardShortcut
基于Shortcut,fyne定义了由键盘操作触发的快捷方式的接口KeyboardShortcut
type KeyboardShortcut interface { Shortcut Key() KeyName Mod() KeyModifier }
fyne中定义了很多键盘快捷方式相关接口,都是继承自该接口
复制/剪切 和 粘贴
type ShortcutCopy struct { KeyboardShortcut Clipboard Clipboard } // Ctrl+C, ShortcutName()返回"Copy" type ShortcutCut struct { KeyboardShortcut Clipboard Clipboard } // Ctrl+X, ShortcutName()返回"Cut" type ShortcutPaste struct { KeyboardShortcut Clipboard Clipboard } // Ctrl+V, ShortcutName()返回"Paste"
撤销 和 重做(反撤销)
type ShortcutUndo struct{ KeyboardShortcut } // Ctrl+Z, ShortcutName()返回"Undo" type ShortcutRedo struct{ KeyboardShortcut } // Ctrl+Y, ShortcutName()返回"Redo"
全选
type ShortcutSelectAll struct{ KeyboardShortcut } // Ctrl+A, ShortcutName()返回"SelectAll"
使用示例
short := &fyne.ShortcutPaste{Clipboard: w.Clipboard()} // 定义一个粘贴操作时的快捷操作 w.Canvas().AddShortcut(short, func(s fyne.Shortcut) { fmt.Println(s.ShortcutName()) }) // 绑定运行函数,当按下Ctrl+V快捷键时会触发