changing the keyCode in a wxKeyEvent

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
wxNewbi
Knows some wx things
Knows some wx things
Posts: 26
Joined: Mon Nov 15, 2004 11:27 pm
Location: London UK
Contact:

changing the keyCode in a wxKeyEvent

Post by wxNewbi » Wed Jan 26, 2005 11:14 am

Hello guy, I am trying to change the value of m_KeyCode in a received wxKeyEvent and then post the same event back to my window. The reason I am doing this is so that my derived TextCtrl will only display Upper case letter even if the user doesn't have the caps lock on.

This is how I have implemented it but it don't propagate to the parent class wxTextctrl so that the charater is displayed. Any one have any ideas, here is my code

Code: Select all


/* myTextCtrl.h */

class myTextCtrl : public wxTextCtrl
{
    public:
    myTextCtrl(wxWindow* parent, wxWindowID id = 1, const wxString& value = _T(""), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long textstyle = myINPUT_HEX);
    ~myTextCtrl();
    
    private:
        long m_textstyle;
		wxString m_Data;
		long CursorPosition;
        void OnKeyPress(wxKeyEvent& event);
    DECLARE_EVENT_TABLE()
};    

Code: Select all

/* myTextCtrl.cpp */
BEGIN_EVENT_TABLE(myTextCtrl,wxTextCtrl)
    EVT_CHAR(myTextCtrl::OnKeyPress)
END_EVENT_TABLE()


myTextCtrl::myTextCtrl(wxWindow* parent, wxWindowID id, const wxString& value , const wxPoint& pos , const wxSize& size, long textstyle)
    : wxTextCtrl(parent,id ,value ,pos,size,wxTE_PROCESS_ENTER) 
{
    m_textstyle = textstyle;
	CursorPosition = 0;
}    

myTextCtrl::~myTextCtrl()
{
}

void myTextCtrl::OnKeyPress(wxKeyEvent& event)
{
    int key;
    char aData;
    //wxString newData;
    key = event.GetKeyCode();
	wxKeyEvent newKey = event;
    if ((m_textstyle == myINPUT_HEX))
    {   
        switch(key)
        { 
                case '1':
	case '2':
	case '3':
	case '4':
	case '5':
	case '6':
	case '7':
	case '8':
	case '9':
	case '0':
	     event.Skip();
	break;
	case 'a':
	case 'b':
	case 'c':
	case 'd':
	case 'e':
                case 'f':
	    aData = (char)key;
	    m_Data = aData;
	    m_Data.UpperCase();
	    aData = m_Data.GetChar(0);
	    newKey.m_keyCode = (int)aData;
	    wxPostEvent((wxEvtHandler *) this, newKey);
                break;
			
	case 'A': // these are processed when the above event
	case 'B': // is generated.
	case 'C':
	case 'D':
	case 'E':
                case 'F':
	    event.Skip();
	break;
                case WXK_BACK:
                case WXK_RETURN:
                case WXK_DELETE:
	case WXK_RIGHT:
	case WXK_UP:
                case WXK_DOWN:          
                case WXK_LEFT:
	     event.Skip();
                break;
	default:
                    wxBell();
                break;
        }
    }
    else if ((m_textstyle == myINPUT_BINARY))
    {   
       switch (key)
       {
          case '1':
          case '0':
			m_Data = GetValue();
            aData = (wxChar)key;
            m_Data.SetChar(aData,GetInsertionPoint());
			m_Data.UpperCase();
			SetValue(m_Data);  
          break;
          case WXK_BACK:
          case WXK_RETURN:
          case WXK_DELETE:
          case WXK_RIGHT:
			  CursorPosition++;
          case WXK_UP:
          case WXK_DOWN:          
          case WXK_LEFT:
			  if( CursorPosition !=0 )
						CursorPosition--;
            event.Skip();
          break;
          default:
            wxBell();
          break;
       }        
    }           
} 
the posted event gets as far as my derived class but it don't propagate further. Any help will be appriciated

thanks
Keep it real, Keep it free, Keep it GPL

thierry
In need of some credit
In need of some credit
Posts: 5
Joined: Tue Jan 18, 2005 7:04 pm
Location: france

Post by thierry » Wed Jan 26, 2005 7:08 pm

You can try something like:

void myTextCtrl::OnChar(wxKeyEvent &event)
{
if ( wxIsprint((int)key) ) {
m_Data.Printf("%c", (char)key);
m_Data.UpperCase();
WriteText( m_Data );
}
else {
event.Skip();
}
}

You don't need to send another event.

Regards

wxNewbi
Knows some wx things
Knows some wx things
Posts: 26
Joined: Mon Nov 15, 2004 11:27 pm
Location: London UK
Contact:

Post by wxNewbi » Thu Jan 27, 2005 9:56 am

Thanks thierry but I have tried that and the problem is that when the user insert a character in any other position from the end, it will always be placed at the end.

I am thinking of using a validator but I am not sure how it works. I was wondering if for a textctrl for instance, when a key is pressed, is it validated before it is printed to the screen? I don't mean the key event, as i know the text validator class checks the key pressed against the current validator setting and rings the wxBell if not allowed. what I am interested in is the 'validate' and transfer functions. When are they called and by whom.

Thanks in advance.
Keep it real, Keep it free, Keep it GPL

Post Reply