📄 inputmethods.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><!-- /home/edba/dist/qtopia/main-Sunday/qtopia/doc/inputmethods.doc:1 --><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><style type="text/css"><!--h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }a:link { color: #004faf; text-decoration: none }a:visited { color: #672967; text-decoration: none }body { background: #ffffff; color: black; }--></style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td width="200" align="left" valign="top"><a href="index.html"><img height="27" width="472" src="dochead.png" border="0"></a><br><font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qtopia</font> <a href="index.html">Home</a> - <a href="qtopiaclasses.html">Classes</a> - <a href="qtopiaannotated.html">Annotated</a> - <a href="qtopiafunctions.html">Functions</a> - <a href="qtindex.html">Qt Embedded</a></td><td align="right" valign="top"> <table border="0" cellpadding="0" cellspacing="0" width="137"> <tr> <td><a href="http://www.trolltech.com/company/about/trolls.html"><img height="100" width="100" src="face.png" border="0"></a></td> <td><img height="100" width="100" src="qtlogo.png" align="top" border="0"></td> </tr> </table></td></tr></table><p> <p> <h2> Input Methods</h2><a name="1"></a><p> The Qtopia 2.x series supports two kinds of input. The first is a widget that generateskeystrokes, such as a virtual keyboard or a handwriting recogniser. The second is a composinginput method. Composing input methods take keyboard input from akeyboard (real or virtual) and compose several keystrokes into onecharacter. Composing input methods can be used to input Chinese,Japanese and Korean text.<p> Input methods built using the Qtopia 2.x series interface can be eithercomposing or virtual keyboard, but cannot be installed on Qtopia1.5-based devices.<p> <h3> Qtopia 2.x series methods</h3><a name="1-1"></a><p> These input methods must supply a QWidget that will be shown on the screen and emit a signal when a key is pressed:<p> <pre>#include <qwidget.h>class SimpleInputMethod : public QWidget{ Q_OBJECTpublic: SimpleInputMethod( QWidget *parent, const char *name, WFlags f );signals: void keyPress( ushort unicode, ushort keycode, ushort modifiers, bool press, bool repeat )};</pre> <p> The parameters of the keyPress signal are:<p> <center><table cellpadding="4" cellspacing="2" border="0"><tr bgcolor="#a2c511"> <th valign="top">Parameter <th valign="top">Notes<tr bgcolor="#f0f0f0"> <td valign="top">unicode<td valign="top">The unicode value of the character, or 0xFFFF if it is a non-printing key.<tr bgcolor="#d0d0d0"> <td valign="top">keycode<td valign="top">The key code as specified in <tt>qnamespace.h</tt><tr bgcolor="#f0f0f0"> <td valign="top">modifiers<td valign="top">A combination of zero or more of the following OR'ed together:Qt::ShiftButton, Qt::ControlButton and Qt::AltButton <tr bgcolor="#d0d0d0"> <td valign="top">press<td valign="top">TRUE for a key press, FALSE for a key release.<tr bgcolor="#f0f0f0"> <td valign="top">repeat<td valign="top">TRUE if this is a repeating keypress.</table></center><p> Additional input methods may be added to Qtopia via plugins. In order to write aninput method plugin you must create an interface to your input method byderiving from the <a href="inputmethodinterface.html">InputMethodInterface</a> class and implementing the purevirtual functions.<p> To make an input method plugin the following implementation is required:<p> <pre>#include <qpe/inputmethodinterface.h>class SimpleInputMethodImpl : public <a href="inputmethodinterface.html">InputMethodInterface</a>{public: SimpleInputMethodImpl(); virtual ~SimpleInputMethodImpl();#ifndef QT_NO_COMPONENT QRESULT queryInterface( const <a href="quuid.html">QUuid</a>&, QUnknownInterface** ); Q_REFCOUNT#endif virtual QWidget *inputMethod( QWidget *parent, Qt::WFlags f ); virtual void resetState(); virtual QPixmap *icon(); virtual QString name(); virtual void onKeyPress( QObject *receiver, const char *slot );private: SimpleInputMethod *input; QPixmap *icn; ulong ref;};</pre> <p> The constructor and destructor are very simple:<p> <pre>SimpleInputMethodImpl::SimpleInputMethodImpl() : input(0), icn(0), ref(0){}SimpleInputMethodImpl::~SimpleInputMethodImpl(){ delete input; delete icn;}</pre> <p> The queryInterface() function can be implemented using thefollowing boilerplate code:<p> <pre>QRESULT SimpleInputMethodImpl::queryInterface( const <a href="quuid.html">QUuid</a> &uuid, QUnknownInterface **iface ){ *iface = 0; if ( uuid == IID_QUnknown ) *iface = this; else if ( uuid == IID_InputMethod ) *iface = this; else return QS_FALSE; (*iface)->addRef(); return QS_OK;}</pre> <p> The inputMethod() function returns the input method widget. Thiswidget will be display on the screen when the user needs to inputtext. You should always return the same widget if this function is calledmultiple times.<p> <pre>QWidget *SimpleInputMethodImpl::inputMethod( QWidget *parent, Qt::WFlags f ){ if ( !input ) input = new SimpleInputMethod( parent, "SimpleInput", f ); return input;}</pre> <p> The resetState() function should return the input method to its defaultstate.<p> The name() function returns the name of the input method. This willbe displayed in the popup list of available input methods.<p> <pre>QString SimpleInputMethodImpl::name(){ return qApp->translate( "InputMethods", "SimpleInput" );}</pre> <p> The name() function returns the icon for the input method. This willbe displayed in the taskbar when the input method is selected.<p> <pre>QPixmap *SimpleInputMethodImpl::icon(){ if ( !icn ) icn = new QPixmap( your pixmap ); return icn;}</pre> <p> The onKeyPress() function must connect the supplied slot to the signalthat is emitted when a key press is generated.<p> The following code will connect the signal to the supplied slot:<p> <pre>void SimpleInputMethodImpl::onKeyPress( QObject *receiver, const char *slot ){ if ( input ) QObject::connect( input, SIGNAL(keyPress(ushort,ushort,ushort,bool,bool)), receiver, slot );}</pre> <p> You must also create an instance of the input method plugin using thefollowing boilerplate code:<p> <pre>Q_EXPORT_INTERFACE(){ Q_CREATE_INSTANCE( SimpleInputMethodImpl )}</pre> <p> The plugin must be compiled as a shared library and placed in the <tt>$QPEDIR/plugins/imputmethods</tt> directory. The following <tt>tmake</tt> projectfile will create a suitable Makefile:<p> <pre>TEMPLATE = libCONFIG += qt warn_on releaseHEADERS = simpleinputmethod.h \ simpleinputmethodimpl.hSOURCES = simpleinputmethod.cpp \ simpleinputmethodimpl.cppTARGET = simpleinputmethodDESTDIR = $(QPEDIR)/plugins/inputmethodsINCLUDEPATH += $(QPEDIR)/includeLIBS += -lqpeVERSION = 1.0.0</pre> <p> Examples of Qtopia input methods can be found in the <tt>inputmethods/</tt>directory of the Qtopia source code.<p> <h3> Composing input methods</h3><a name="1-2"></a><p> Qtopia 2.x series input methods are created by inheriting from<a href="extinputmethodinterface.html">ExtInputMethodInterface</a>, in the same manner as <a href="inputmethodinterface.html">InputMethodInterface</a>.<p> The main new feature of ExtInputMethodInterface provides support forcomposing input methods. The input method itself is implemented as aclass that inherits from QWSInputMethod. It filters all keyboardevents before they are sent to the application that has keyboardfocus.<p> There is a complete example of a composing input method in the<tt>examples/inputmethod</tt> directory of Qtopia.<p> The main class of a composing input method inherits from QWSInputMethod. Itfilters all keyboard events before they are sent to the application thathas keyboard focus. A minimal input method could look like this:<p> <pre>class ComposeIM : public QWSInputMethod{public: ComposeIM(); void reset(); bool filter(int unicode, int keycode, int modifiers, bool isPress, bool autoRepeat); enum State { Off, On };private: State state; QString composed;};</pre> <p> The QWSInputMethod::filter() function is the central part of the inputmethod. It implements the composition logic and maintains state. Ituses QWSInputMethod::sendIMEvent() to send input method events.<p> The function QWSInputMethod::reset() is called from the system whenthe input method needs to reset state, eg. when the focus widgetchanges.<p> Other functions include QWSInputMethod::setMicroFocus() which iscalled when the cursor position changes inside the focus widget, andQWSInputMethod::mouseHandler() which is called when the user clicksinside the composed text. <p> A plugin for the <a href="extinputmethodinterface.html">ExtInputMethodInterface</a> requires the following implementation:<p> <pre>class ComposeImpl : public <a href="extinputmethodinterface.html">ExtInputMethodInterface</a>{public: ComposeImpl(); virtual ~ComposeImpl();#ifndef QT_NO_COMPONENT QRESULT queryInterface( const <a href="quuid.html">QUuid</a>&, QUnknownInterface** ); Q_REFCOUNT#endif virtual QString name(); virtual QPixmap *icon(); virtual void resetState(); virtual QStringList compatible(); virtual QWSInputMethod *inputMethod( ); virtual QWidget *statusWidget( QWidget *parent, Qt::WFlags f); virtual QWidget *keyboardWidget( QWidget *parent, Qt::WFlags f); virtual void qcopReceive( const QCString &msg, const QByteArray &data );private: ComposeIM *input; QPixmap *icn; QWidget *statWid; ulong ref;};</pre> <p> The queryInterface() function for this interface can be implemented as follows:<p> <pre>QRESULT ComposeImpl::queryInterface( const <a href="quuid.html">QUuid</a> &uuid, QUnknownInterface **iface ){ *iface = 0; if ( uuid == IID_QUnknown ) *iface = this; else if ( uuid == IID_ExtInputMethod ) *iface = this; else return QS_FALSE; (*iface)->addRef(); return QS_OK;}</pre> <p> For a composing input method, the keyboardWidget() function returns 0:<pre>QWidget *ComposeImpl::keyboardWidget( QWidget *, Qt::WFlags ){ return 0;}</pre> <p> The function inputMethod() returns the input method:<pre>QWSInputMethod *ComposeImpl::inputMethod( ){ if ( !input ) input = new ComposeIM( ); return input;}</pre> <p> resetState() resets the state of the input method:<pre>void ComposeImpl::resetState(){ if ( input ) input->reset();}</pre> <p> icon() returns the icon.<pre>QPixmap *ComposeImpl::icon(){ if (!icn) icn = new QPixmap( \e pixmap ); return icn;}</pre> <p> name() returns the name:<pre>QString ComposeImpl::name(){ return qApp->translate( "InputMethods", "Latest" );}</pre> <p> For a composing input method, the widget returned by statusWidget()will be placed in the header when the input method is selected. Thiswidget is typically used to display status, and can also be used tolet the user interact with the input method.<pre>QWidget *ComposeImpl::statusWidget( QWidget *parent, Qt::WFlags ){ if (!statWid) { (void) inputMethod(); //create input before we use it statWid = new IMStatus( input, parent); } return statWid;}</pre> <p> The compatible() function can be used to say that this input method isonly compatible with certain other input methods. In this case, thereare no restrictions:<pre>QStringList ComposeImpl::compatible( ){ return QStringList();}</pre> <p> In qcopReceive(), we get notified when there is an event on theinputmethod channel:<pre>void ComposeImpl::qcopReceive( const QCString &msg, const QByteArray &data ){ //process QCop event}</pre> <p> The following messages are defined:<p> <ul><li> inputMethodHint(int)One of the standard hints defined by QPEApplication::InputMethodHint,except QPEApplication::Named (see next item).<li> inputMethodHint(QString)A named hint. These are input method specific. If the input methoddoes not recognize the hint, it should attempt to provide input ofthe largest range of characters it supports.</ul><p> The Phone Key Input Method allows installable named hints. A named hint isdefined by the Buttons section of the<a href="config.html">Config</a> file <tt>/opt/Qtopia/etc/im/pkim/named_</tt> name .conf, and takesthe same form defined for the TextButtons sections for that input method.The Security settings use a "netmask" named hint, for example.<p> <!-- eof --><p><address><hr><div align="center"><table width="100%" cellspacing="0" border="0"><tr><td>Copyright © 2001-2004 Trolltech<td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a><td align="right"><div align="right">Qtopia version 2.0.0</div></table></div></address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -