Forest Logo
search
package_2

KeyGlyph

By @stratizverified

Roblox

KeyGlyph

Draws the glyph for a key, so a UI only passes a KeyCode and gets a frame back.

  • Gamepad buttons use Roblox's own images from UserInputService:GetImageForKeyCode.
  • Keyboard keys and mouse inputs use a drawn key cap from a sprite sheet, cut to the key itself, so there is no padding around it.
  • A key the sheet has no drawing for gets Roblox's blank key cap with the key's character on it, from UserInputService:GetStringForKeyCode, so it follows the keyboard's layout.

A glyph keeps the height it was made with and takes the key's width, so Space draws wide and a letter draws square.

Usage

local KeyGlyph = require(path.to.KeyGlyph)

local glyph = KeyGlyph.new(30, Enum.KeyCode.E)
glyph.Instance.Parent = someFrame

glyph:Set(Enum.KeyCode.ButtonX) -- swap the key
glyph:Set(nil) -- hide it

glyph:Destroy()

For UI built some other way, like a React component, KeyGlyph:GetLook returns what to draw without making an instance.

API

KeyGlyph.new(height, input)

Returns a new glyph whose Instance is an unparented ImageLabel, height px tall, showing input. A nil input leaves it hidden until Set fills it. Position, name and parent are up to the caller.

KeyGlyph:Set(input)

Shows another input and fits the glyph's width to the key's shape. A nil input hides it.

KeyGlyph:GetLook(input)

Returns { Image, ImageRectOffset, ImageRectSize, AspectRatio, Text? } for a KeyCode or a mouse UserInputType, or nil when there is nothing to draw. Text is set only for the lettered blank cap. It works on the module as well as on a glyph.

KeyGlyph:Destroy()

Destroys the glyph's instance.

Credits

The keyboard sprite sheet and its cell layout come from Quenty's InputImageLibrary in NevermoreEngine (MIT).

Package Details

Install command (Click to copy)


Version

1.0.0

License

MIT

check_circle

Safe for commercial use

Automated license review — not legal advice.