Text Input With OBSE
This article has been nominated to be a Featured Article. This means that it has been found to be of high quality, and input on whether or not it is qualified to be Featured is desired. Give your opinion on the Talk Page!
This code will accept keystrokes from the player, and display the text they write in a MessageBox. It will properly account for the Shift key and the Backspace key, and it will do so without causing any bloating whatsoever.
Setup
First of all, this script requires the Oblivion Script Extender, v0014 or higher.
The script stores the string in the name of an object - you need an object defined for this purpose. This object may be anything that has a name, though it is highly recommended that the player never see it, or if they do, make sure it is after they have named it. An activator is most likely the best choice.
The string must have an initial value that ends in an accent mark (`). I used "`Enter Name`".
To use any other name, the script must be changed in five places - and these five places must have the same value. These five places have been marked with comments stating "!!!INIT NAME!!!'".
Additionally, the name must have a trailing tilde (~) in two of these places. They have been marked "w/ ~" and the other three have been marked "w/o ~".
Finally, the name cannot be "`~~`", unless you change the places where this string is used in the script. It is used in six places, in two sets of three.
Further, you also need a separate Activator to actually put the script on. This activator may be placed somewhere for the player to activate it, or else it must be placed somewhere, the Reference marked Persistent, and given a Reference ID so that the script can be activated from another script.
Limitations
As discussed above, the initial name of the object, as defined by the script, has some special rules. See the Setup section for more information.
The script as-is only supports Roman letters, spaces, dashes, and apostrophes. Support for additional characters is relatively simple, but keep in mind that you will have to do this yourself if you desire it.
Further, Caps Lock is ignored. Given that it is generally used for running, this is probably preferable, but supporting Caps Lock could be added to the script with little effort.
Finally, it is impossible to support the accent mark (`), tilde (~), semi-colon (;) or double-quotation mark ("). The first two cannot be supported because it is impossible for the player to actually hit that key without opening the Console - even if it is disabled. The last two cannot be supported simply because it is impossible to include one within a string in a script, as each ends the string (and Oblivion does not include any escape character for them).
Relevant Functions
The following functions are used in this code:
- GameMode
- OnActivate
- Activate
- SetName
- ModName
- AppendToName
- OnKeyDown
- IsKeyPressed3
- MessageBoxEX
- Message
- GetButtonPressed
It is recommended that you have some idea of what these functions do and how they work.
The Code
scn TextInputBox ; Object script, goes on an Activator short state short caps short button ref StringVar short bConsole Begin GameMode if ( state ) Activate player 1 endif End Begin MenuMode if ( state ) Activate player 1 endif End Begin OnActivate if ( state == 0 ) OnKeyDown 57 ; clears the counter from the initial press when activating the item GetButtonPressed ; clears the counter as well if ( StringVar == 0 ) set StringVar to INSERT_NAME_STORAGE_OBJECT_ID_HERE ; this is where your string gets stored endif SetName "`Enter Name`~" StringVar ; !!!INIT NAME!!! w/ ~ set state to 1 elseif ( state == 1 ) ModName "~|" StringVar MessageBoxEx "%n|Done" StringVar ; the specific text can be changed to meet your needs ModName "`Enter Name`|" StringVar ; !!!INIT NAME!!! w/o ~ AppendToName "~" StringVar set state to 2 elseif ( state == 2 ) ; first, standard MessageBox menu stuffs set button to GetButtonPressed + 1 if ( button ) ; pressed Done set state to 3 return endif ; check for Console if ( OnKeyDown 41 ) set bConsole to bConsole == 0 ; toggle bConsole endif if ( bConsole ) return ; return if in the Console (the rest of the commands break the Console) endif ; now String-capturing code set caps to 0 if ( IsKeyPressed3 42 ) || ( IsKeyPressed3 54 ) ; either Shift key set caps to 1 endif if ( OnKeyDown 16 ) if ( caps ) ModName "~|Q~" StringVar else ModName "~|q~" StringVar endif set state to 1 elseif ( OnKeyDown 17 ) if ( caps ) ModName "~|W~" StringVar else ModName "~|w~" StringVar endif set state to 1 elseif ( OnKeyDown 18 ) if ( caps ) ModName "~|E~" StringVar else ModName "~|e~" StringVar endif set state to 1 elseif ( OnKeyDown 19 ) if ( caps ) ModName "~|R~" StringVar else ModName "~|r~" StringVar endif set state to 1 elseif ( OnKeyDown 20 ) if ( caps ) ModName "~|T~" StringVar else ModName "~|t~" StringVar endif set state to 1 elseif ( OnKeyDown 21 ) if ( caps ) ModName "~|Y~" StringVar else ModName "~|y~" StringVar endif set state to 1 elseif ( OnKeyDown 22 ) if ( caps ) ModName "~|U~" StringVar else ModName "~|u~" StringVar endif set state to 1 elseif ( OnKeyDown 23 ) if ( caps ) ModName "~|I~" StringVar else ModName "~|i~" StringVar endif set state to 1 elseif ( OnKeyDown 24 ) if ( caps ) ModName "~|O~" StringVar else ModName "~|o~" StringVar endif set state to 1 elseif ( OnKeyDown 25 ) if ( caps ) ModName "~|P~" StringVar else ModName "~|p~" StringVar endif set state to 1 elseif ( OnKeyDown 30 ) if ( caps ) ModName "~|A~" StringVar else ModName "~|a~" StringVar endif set state to 1 elseif ( OnKeyDown 31 ) if ( caps ) ModName "~|S~" StringVar else ModName "~|s~" StringVar endif set state to 1 elseif ( OnKeyDown 32 ) if ( caps ) ModName "~|D~" StringVar else ModName "~|d~" StringVar endif set state to 1 elseif ( OnKeyDown 33 ) if ( caps ) ModName "~|F~" StringVar else ModName "~|f~" StringVar endif set state to 1 elseif ( OnKeyDown 34 ) if ( caps ) ModName "~|G~" StringVar else ModName "~|g~" StringVar endif set state to 1 elseif ( OnKeyDown 35 ) if ( caps ) ModName "~|H~" StringVar else ModName "~|h~" StringVar endif set state to 1 elseif ( OnKeyDown 36 ) if ( caps ) ModName "~|J~" StringVar else ModName "~|j~" StringVar endif set state to 1 elseif ( OnKeyDown 37 ) if ( caps ) ModName "~|K~" StringVar else ModName "~|k~" StringVar endif set state to 1 elseif ( OnKeyDown 38 ) if ( caps ) ModName "~|L~" StringVar else ModName "~|l~" StringVar endif set state to 1 elseif ( OnKeyDown 44 ) if ( caps ) ModName "~|Z~" StringVar else ModName "~|z~" StringVar endif set state to 1 elseif ( OnKeyDown 45 ) if ( caps ) ModName "~|X~" StringVar else ModName "~|x~" StringVar endif set state to 1 elseif ( OnKeyDown 46 ) if ( caps ) ModName "~|C~" StringVar else ModName "~|c~" StringVar endif set state to 1 elseif ( OnKeyDown 47 ) if ( caps ) ModName "~|V~" StringVar else ModName "~|v~" StringVar endif set state to 1 elseif ( OnKeyDown 48 ) if ( caps ) ModName "~|B~" StringVar else ModName "~|b~" StringVar endif set state to 1 elseif ( OnKeyDown 49 ) if ( caps ) ModName "~|N~" StringVar else ModName "~|n~" StringVar endif set state to 1 elseif ( OnKeyDown 50 ) if ( caps ) ModName "~|M~" StringVar else ModName "~|m~" StringVar endif set state to 1 elseif ( OnKeyDown 12 ) if ( caps ) ModName "~|_~" StringVar else ModName "~|-~" StringVar endif set state to 1 elseif ( OnKeyDown 57 ) ModName "~| ~" StringVar set state to 1 elseif ( OnKeyDown 40 ) ; CANNOT type double-quote mark ModName "~|'~" StringVar set state to 1 elseif ( OnKeyDown 14 ) ; Backspace Label 0 ModName "q~|" StringVar ModName "w~|" StringVar ModName "e~|" StringVar ModName "r~|" StringVar ModName "t~|" StringVar ModName "y~|" StringVar ModName "u~|" StringVar ModName "i~|" StringVar ModName "o~|" StringVar ModName "p~|" StringVar ModName "a~|" StringVar ModName "s~|" StringVar ModName "d~|" StringVar ModName "f~|" StringVar ModName "g~|" StringVar ModName "h~|" StringVar ModName "j~|" StringVar ModName "k~|" StringVar ModName "l~|" StringVar ModName "z~|" StringVar ModName "x~|" StringVar ModName "c~|" StringVar ModName "v~|" StringVar ModName "b~|" StringVar ModName "n~|" StringVar ModName "m~|" StringVar ModName "-~|" StringVar ModName "_~|" StringVar ModName " ~|" StringVar ModName "'~|" StringVar ModName " ~|" StringVar AppendToName "~" StringVar ModName "~~|~" StringVar ModName "|`~~`" StringVar ModName "`~~`~|`Enter Name`~" StringVar ; !!!INIT NAME!!! w/ ~ ModName "`~~`|" StringVar if ( IsKeyPressed3 29 ) || ( IsKeyPressed3 157 ) ; Ctrl+Delete deletes an entire word. if ( CompareName " ~" StringVar != 1 ) if ( CompareName "`Enter Name`" StringVar != 1 ) ; !!!INIT NAME!!! w/o ~ Goto 0 endif endif endif set state to 1 elseif ( OnKeyDown 28 ) ; Enter - ends menu same as Done set state to 3 endif elseif ( state == 3 ) ModName "|`~~`" StringVar if ( CompareName "`~~`~" StringVar ) SetName "`Enter Name`" StringVar ; !!!INIT NAME!!! w/o ~ Message "Must enter a new name." set state to 1 else ModName "`~~`|" StringVar ModName "~|" StringVar set state to 0 set button to 0 ; include any code to use the string here MessageBoxEx "Congratulations, you named it %n" StringVar endif endif End
Using the Script
The text box will appear whenever the object that the script is attached to is Activated. So either place the Activator somewhere that the player will see and activate it, or use this code:
ActivatorRef.Activate player 1
Further, the final block of code should be modified for your needs. It is important to remove the trailing tilde and reset the two short variables, but anything else is possible or optional.
Final Comments
This code has been thoroughly tested, and should work as long as you point StringVar to an acceptable object. Please report any bugs to the Talk page. If you believe you know what changes to the code are necessary to fix the bug, please make these changes, but also please report what was changed and why in the Talk page. Thank you.