Single line input

by Eskel

HTML

<div contenteditable='true' id='talkInput' class='textInput'></div>
<div id='ruler' class='textInput'></div>

CSS

* { box-sizing: border-box }
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
	margin: 0;
	padding: 0;
	border: 0;
	font-size: 100%;
	font: inherit;
	vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
	display: block;
}
body {
	line-height: 1;
}
ol, ul {
	list-style: none;
}
blockquote, q {
	quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
	content: '';
	content: none;
}
table {
	border-collapse: collapse;
	border-spacing: 0;
}
html {
    background: #555;
    color: white;
}
.textInput {
	font-size: 14pt;
    font-family: Arial;
}
#ruler
{
    /*display: none;*/
    display: inline-block;
    border: 1px red solid;
    height: auto;
    width: auto;
    white-space: nowrap;
}
#talkInput {
	display: block;
	height: 30px;
	width: 100px;
	pointer-events: auto;
	background: none;
	text-align: center;
	color: white;
	margin: auto;
	outline: none;
	border-bottom: 2px gray solid;
	cursor: text;
	overflow: hidden;
    white-space: nowrap;
}

CoffeeScript

console.clear()
input = $('#talkInput')
ruler = $('#ruler')

keys = [8, 9, 13, 16, 17, 18, 19, 20, 27]

input.on 'keyup', (e) ->
    ruler.text input.text()

input.on 'keypress', (e) ->
    if $.inArray( e.keyCode, keys ) is -1
        ruler.append String.fromCharCode(e.charCode)
        if window.getSelection().isCollapsed
            if ruler.get(0).offsetWidth >= input.width()
                e.preventDefault()
                e.stopPropagation()

input.on 'paste', (e) ->
    e.preventDefault()
    e.stopPropagation()