text_input_box.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*******************************************************************************************
  2. *
  3. * raylib [text] example - input box
  4. *
  5. * Example complexity rating: [★★☆☆] 2/4
  6. *
  7. * Example originally created with raylib 1.7, last time updated with raylib 3.5
  8. *
  9. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  10. * BSD-like license that allows static linking with closed source software
  11. *
  12. * Copyright (c) 2017-2025 Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. #define MAX_INPUT_CHARS 9
  17. //------------------------------------------------------------------------------------
  18. // Program main entry point
  19. //------------------------------------------------------------------------------------
  20. int main(void)
  21. {
  22. // Initialization
  23. //--------------------------------------------------------------------------------------
  24. const int screenWidth = 800;
  25. const int screenHeight = 450;
  26. InitWindow(screenWidth, screenHeight, "raylib [text] example - input box");
  27. char name[MAX_INPUT_CHARS + 1] = "\0"; // NOTE: One extra space required for null terminator char '\0'
  28. int letterCount = 0;
  29. Rectangle textBox = { screenWidth/2.0f - 100, 180, 225, 50 };
  30. bool mouseOnText = false;
  31. int framesCounter = 0;
  32. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  33. //--------------------------------------------------------------------------------------
  34. // Main game loop
  35. while (!WindowShouldClose()) // Detect window close button or ESC key
  36. {
  37. // Update
  38. //----------------------------------------------------------------------------------
  39. if (CheckCollisionPointRec(GetMousePosition(), textBox)) mouseOnText = true;
  40. else mouseOnText = false;
  41. if (mouseOnText)
  42. {
  43. // Set the window's cursor to the I-Beam
  44. SetMouseCursor(MOUSE_CURSOR_IBEAM);
  45. // Get char pressed (unicode character) on the queue
  46. int key = GetCharPressed();
  47. // Check if more characters have been pressed on the same frame
  48. while (key > 0)
  49. {
  50. // NOTE: Only allow keys in range [32..125]
  51. if ((key >= 32) && (key <= 125) && (letterCount < MAX_INPUT_CHARS))
  52. {
  53. name[letterCount] = (char)key;
  54. name[letterCount+1] = '\0'; // Add null terminator at the end of the string
  55. letterCount++;
  56. }
  57. key = GetCharPressed(); // Check next character in the queue
  58. }
  59. if (IsKeyPressed(KEY_BACKSPACE))
  60. {
  61. letterCount--;
  62. if (letterCount < 0) letterCount = 0;
  63. name[letterCount] = '\0';
  64. }
  65. }
  66. else SetMouseCursor(MOUSE_CURSOR_DEFAULT);
  67. if (mouseOnText) framesCounter++;
  68. else framesCounter = 0;
  69. //----------------------------------------------------------------------------------
  70. // Draw
  71. //----------------------------------------------------------------------------------
  72. BeginDrawing();
  73. ClearBackground(RAYWHITE);
  74. DrawText("PLACE MOUSE OVER INPUT BOX!", 240, 140, 20, GRAY);
  75. DrawRectangleRec(textBox, LIGHTGRAY);
  76. if (mouseOnText) DrawRectangleLines((int)textBox.x, (int)textBox.y, (int)textBox.width, (int)textBox.height, RED);
  77. else DrawRectangleLines((int)textBox.x, (int)textBox.y, (int)textBox.width, (int)textBox.height, DARKGRAY);
  78. DrawText(name, (int)textBox.x + 5, (int)textBox.y + 8, 40, MAROON);
  79. DrawText(TextFormat("INPUT CHARS: %i/%i", letterCount, MAX_INPUT_CHARS), 315, 250, 20, DARKGRAY);
  80. if (mouseOnText)
  81. {
  82. if (letterCount < MAX_INPUT_CHARS)
  83. {
  84. // Draw blinking underscore char
  85. if (((framesCounter/20)%2) == 0) DrawText("_", (int)textBox.x + 8 + MeasureText(name, 40), (int)textBox.y + 12, 40, MAROON);
  86. }
  87. else DrawText("Press BACKSPACE to delete chars...", 230, 300, 20, GRAY);
  88. }
  89. EndDrawing();
  90. //----------------------------------------------------------------------------------
  91. }
  92. // De-Initialization
  93. //--------------------------------------------------------------------------------------
  94. CloseWindow(); // Close window and OpenGL context
  95. //--------------------------------------------------------------------------------------
  96. return 0;
  97. }
  98. // Check if any key is pressed
  99. // NOTE: We limit keys check to keys between 32 (KEY_SPACE) and 126
  100. bool IsAnyKeyPressed()
  101. {
  102. bool keyPressed = false;
  103. int key = GetKeyPressed();
  104. if ((key >= 32) && (key <= 126)) keyPressed = true;
  105. return keyPressed;
  106. }