📄 6-test-fly-anonym.html
字号:
<HTML>
<HEAD>
<SCRIPT type="text/javascript" src="vkboard.js"></SCRIPT>
<SCRIPT><!--
// This example is based on the previous ('4-test-fly.html')
// sample and (also) shows a way to handle multiple INPUT
// fields with a single vkeyboard. The only difference is
// that now we don't need fields' id's: script finds and
// enumerates all INPUTs automatically.
// 'source' is the field which is currently focused:
var source = null, insertionS = 0, insertionE = 0;
var userstr = navigator.userAgent.toLowerCase();
var isgecko = (userstr.indexOf('gecko') != -1) && (userstr.indexOf('applewebkit') == -1);
function search_for_text_field(num)
{
var tg = document.getElementsByTagName("INPUT");
if(tg && tg[num])
return tg[num];
else
{
var tg2 = document.getElementsByTagName("TEXTAREA");
if(tg2 && tg2[tg.length - num])
return tg2[tg.length - num];
}
return null;
}
// This function retrieves the source element
// for the given event object:
function get_event_source(e)
{
var event = e || window.event;
return event.srcElement || event.target;
}
// This function binds 'handler' function to the
// 'eventType' event of the 'elem' element:
function setup_event(elem, eventType, handler)
{
return (elem.attachEvent) ? elem.attachEvent("on" + eventType, handler) : ((elem.addEventListener) ? elem.addEventListener(eventType, handler, false) : false);
}
// By focusing the INPUT field we set the 'source'
// to the newly focused field:
function focus_keyboard(e)
{
source = get_event_source(e);
}
// This function slightly differs from one with the same name
// in '4-test-fly' sample. Now it accepts not the id, but the
// number (index in the INPUT elements array) of the INPUT field.
function register_input_field(num)
{
var tg = document.getElementsByTagName("INPUT");
if(tg && tg[num])
setup_event(tg[num], "focus", focus_keyboard);
}
// The same for TEXTAREA fields:
function register_textarea_field(num)
{
var tg = document.getElementsByTagName("TEXTAREA");
if(tg && tg[num])
setup_event(tg[num], "focus", focus_keyboard);
}
// This function enumerates and "registers" all INPUT fields
// on the page:
function register_text_fields()
{
var tg = document.getElementsByTagName("INPUT");
if(tg)
{
for(var i = 0; i < tg.length; i++)
register_input_field(i);
}
tg = document.getElementsByTagName("TEXTAREA");
if(tg)
{
for(var i = 0; i < tg.length; i++)
register_textarea_field(i);
}
}
function init()
{
// Note: all parameters, starting with 3rd, in the following
// expression are equal to the default parameters for the
// VKeyboard object. The only exception is 15th parameter
// (flash switch), which is false by default.
new VKeyboard("keyboard", // container's id
keyb_callback, // reference to the callback function
true, // create the numpad or not? (this and the following params are optional)
"", // font name ("" == system default)
"14px", // font size in px
"#000", // font color
"#F00", // font color for the dead keys
"#FFF", // keyboard base background color
"#FFF", // keys' background color
"#DDD", // background color of switched/selected item
"#777", // border color
"#CCC", // border/font color of "inactive" key (key with no value/disabled)
"#FFF", // background color of "inactive" key (key with no value/disabled)
"#F77", // border color of the language selector's cell
true, // show key flash on click? (false by default)
"#CC3300", // font color during flash
"#FF9966", // key background color during flash
"#CC3300", // key border color during flash
false, // embed VKeyboard into the page?
true, // use 1-pixel gap between the keys?
0); // index(0-based) of the initial layout
// The very 1st (index == 0) field is "focused" by default:
source = search_for_text_field(0);
// Any INPUTs? Register them all!
if(source) register_text_fields();
source.focus();
}
// Advanced callback function:
//
function keyb_callback(ch)
{
var val = source.value;
switch(ch)
{
case "BackSpace":
if(val.length)
{
var span = null;
if(document.selection)
span = document.selection.createRange().duplicate();
if(span && span.text.length > 0)
{
span.text = "";
getCaretPositions(source);
}
else
deleteAtCaret(source);
}
break;
default:
insertAtCaret(source, (ch == "Enter" ? (window.opera ? '\r\n' : '\n') : ch));
}
}
// This function retrieves the position (in chars, relative to
// the start of the text) of the edit cursor (caret), or, if
// text is selected in the TEXTAREA, the start and end positions
// of the selection.
//
function getCaretPositions(ctrl)
{
var CaretPosS = -1, CaretPosE = 0;
// Mozilla way:
if(ctrl.selectionStart || (ctrl.selectionStart == '0'))
{
CaretPosS = ctrl.selectionStart;
CaretPosE = ctrl.selectionEnd;
insertionS = CaretPosS == -1 ? CaretPosE : CaretPosS;
insertionE = CaretPosE;
}
// IE way:
else if(document.selection && ctrl.createTextRange)
{
var start = end = 0;
try
{
start = Math.abs(document.selection.createRange().moveStart("character", -10000000)); // start
if (start > 0)
{
try
{
var endReal = Math.abs(ctrl.createTextRange().moveEnd("character", -10000000));
var r = document.body.createTextRange();
r.moveToElementText(ctrl);
var sTest = Math.abs(r.moveStart("character", -10000000));
var eTest = Math.abs(r.moveEnd("character", -10000000));
if ((ctrl.tagName.toLowerCase() != 'input') && (eTest - endReal == sTest))
start -= sTest;
}
catch(err) {}
}
}
catch (e) {}
try
{
end = Math.abs(document.selection.createRange().moveEnd("character", -10000000)); // end
if(end > 0)
{
try
{
var endReal = Math.abs(ctrl.createTextRange().moveEnd("character", -10000000));
var r = document.body.createTextRange();
r.moveToElementText(ctrl);
var sTest = Math.abs(r.moveStart("character", -10000000));
var eTest = Math.abs(r.moveEnd("character", -10000000));
if ((ctrl.tagName.toLowerCase() != 'input') && (eTest - endReal == sTest))
end -= sTest;
}
catch(err) {}
}
}
catch (e) {}
insertionS = start;
insertionE = end
}
}
function setRange(ctrl, start, end)
{
if(ctrl.setSelectionRange) // Standard way (Mozilla, Opera, ...)
{
ctrl.setSelectionRange(start, end);
}
else // MS IE
{
var range;
try
{
range = ctrl.createTextRange();
}
catch(e)
{
try
{
range = document.body.createTextRange();
range.moveToElementText(ctrl);
}
catch(e)
{
range = null;
}
}
if(!range) return;
range.collapse(true);
range.moveStart("character", start);
range.moveEnd("character", end - start);
range.select();
}
insertionS = start;
insertionE = end;
}
function deleteSelection(ctrl)
{
if(insertionS == insertionE) return;
var tmp = (document.selection && !window.opera) ? ctrl.value.replace(/\r/g,"") : ctrl.value;
ctrl.value = tmp.substring(0, insertionS) + tmp.substring(insertionE, tmp.length);
setRange(ctrl, insertionS, insertionS);
}
function deleteAtCaret(ctrl)
{
// if(insertionE < insertionS) insertionE = insertionS;
if(insertionS != insertionE)
{
deleteSelection(ctrl);
return;
}
if(insertionS == insertionE)
insertionS = insertionS - 1;
var tmp = (document.selection && !window.opera) ? ctrl.value.replace(/\r/g,"") : ctrl.value;
ctrl.value = tmp.substring(0, insertionS) + tmp.substring(insertionE, tmp.length);
setRange(ctrl, insertionS, insertionS);
}
// This function inserts text at the caret position:
//
function insertAtCaret(ctrl, val)
{
if(insertionS != insertionE) deleteSelection(ctrl);
if(isgecko && document.createEvent && !window.opera)
{
var e = document.createEvent("KeyboardEvent");
if(e.initKeyEvent && ctrl.dispatchEvent)
{
e.initKeyEvent("keypress", // in DOMString typeArg,
false, // in boolean canBubbleArg,
true, // in boolean cancelableArg,
null, // in nsIDOMAbstractView viewArg, specifies UIEvent.view. This value may be null;
false, // in boolean ctrlKeyArg,
false, // in boolean altKeyArg,
false, // in boolean shiftKeyArg,
false, // in boolean metaKeyArg,
null, // key code;
val.charCodeAt(0));// char code.
ctrl.dispatchEvent(e);
}
}
else
{
var tmp = (document.selection && !window.opera) ? ctrl.value.replace(/\r/g,"") : ctrl.value;
ctrl.value = tmp.substring(0, insertionS) + val + tmp.substring(insertionS, tmp.length);
}
setRange(ctrl, insertionS + val.length, insertionS + val.length);
}
//--></SCRIPT></HEAD>
<BODY onload="init()">
<P style="font-family:Tahoma;font-size:14px">Virtual keyboard test #6: changing target input field (anonymous, no ID) on the fly.<BR>
<SMALL>Text is streamed to the field last focused.</SMALL></P>
<INPUT type="text" size="40" onkeyup="getCaretPositions(this);" onclick="getCaretPositions(this);"></INPUT><BR><BR>
<INPUT type="text" size="40" onkeyup="getCaretPositions(this);" onclick="getCaretPositions(this);"></INPUT><BR><BR>
<TEXTAREA type="text" rows="15" cols="34" onkeyup="getCaretPositions(this);" onclick="getCaretPositions(this);"></TEXTAREA><BR><BR>
<DIV id="keyboard"></DIV>
</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -