Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Letters only in Text box? #130

Closed
Majed93 opened this issue Feb 11, 2015 · 8 comments
Closed

Letters only in Text box? #130

Majed93 opened this issue Feb 11, 2015 · 8 comments

Comments

@Majed93
Copy link

Majed93 commented Feb 11, 2015

Is there a way to only allow letters in ImGui::InputText?

@ocornut
Copy link
Owner

ocornut commented Feb 11, 2015

No but that could be added very easily following what ImGuiInputTextFlags_CharsHexadecimal is doing.

Flag definition in imgui.h

ImGuiInputTextFlags_CharsHexadecimal    = 1 << 1,   // Allow 0123456789ABCDEFabcdef

Code in imgui.cpp, InputTextFilterCharacter()

if (flags & ImGuiInputTextFlags_CharsHexadecimal)
    if (!(c >= '0' && c <= '9') && !(c >= 'a' && c <= 'f') && !(c >= 'A' && c <= 'F'))
        return true;

Would you like a-zA-Z only and not anything else?

@Majed93
Copy link
Author

Majed93 commented Feb 11, 2015

Yes just A-Z, also is there a way so it's only capital letters?

Thanks

@ocornut
Copy link
Owner

ocornut commented Feb 11, 2015

A-Z is a bit specific so I have added a character filtering system so you can filter characters yourself.

// turn input into uppercase
ImGui::InputText("uppercase", buf, 64, ImGuiInputTextFlags_CharsUppercase);

// custom filter, e.g:remove space
static int FilterNoSpace(ImGuiTextEditCallbackData* data)
{ 
    if (data->EventChar == ' ')  return 1;
    return 0;
}

ImGui::InputText("custom: no spaces", buf5, 64, ImGuiInputTextFlags_CallbackCharFilter, FilterNoSpace);

@ocornut
Copy link
Owner

ocornut commented Feb 11, 2015

Here's a callback function that does the filtering you want

                static int FilterAZ(ImGuiTextEditCallbackData* data) 
                { 
                    ImWchar c = data->EventChar;
                    if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
                        return 1;
                    data->EventChar += 'A'-'a';
                    return 0;
                } 

@Majed93
Copy link
Author

Majed93 commented Feb 11, 2015

Got it working now! Thank you so much! :)

@Majed93 Majed93 closed this as completed Feb 11, 2015
@Majed93 Majed93 reopened this Feb 12, 2015
@Majed93
Copy link
Author

Majed93 commented Feb 12, 2015

Just noticed a little bug. We you put cap locks on and type letters, they are outputted as numbers or symbols.

@ocornut
Copy link
Owner

ocornut commented Feb 12, 2015

Yes sorry the function i wrote above wasn't tested and its wrong. Just increment the character if its a lowercase.

@ocornut
Copy link
Owner

ocornut commented Feb 12, 2015

e.g. (sorry it's hard typing code in the github box)

static int FilterAZ(ImGuiTextEditCallbackData* data) 
{ 
    ImWchar c = data->EventChar;
    if (c >= 'A' && c <= 'Z') return 0;
    if (c >= 'a' && c <= 'z') { data->EventChar += 'A'-'a'; return 0; }
    return 1;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants